編程程序中有一系列圖片讓我給封裝到List中了,然后儲存為硬碟123.txt檔案了,打算后期方便讀取。
一、原先的思路是將這個123.txt檔案一起發給用戶,讀取時真接用FileStream讀取。
經測驗,下面代碼是可用的
string Spath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\123.txt";
FileStream stream = new FileStream(Spath, FileMode.Open);
BinaryFormatter bFormat = new BinaryFormatter();
CardList = (List<Tuple<string, Bitmap>>)bFormat.Deserialize(stream);//反序列化得到的是一個object物件.必須做下型別轉換
stream.Close();
二、后來我突發奇想,何不把123.txt檔案加載到類資源里去呢(類別庫右鍵屬性-->資源-->添加資源-->添加現有檔案,加載123.txt)
然后通過Properties.Resources.ResourceManager.GetObject("123").ToString();獲取內容,但經測驗,下列代碼不成功
string Str = Properties.Resources.ResourceManager.GetObject("123").ToString();
byte[] array = Encoding.ASCII.GetBytes(Str);
MemoryStream stream = new MemoryStream(array);
BinaryFormatter bFormat = new BinaryFormatter();
CardList = (List<Tuple<string, Bitmap>>)bFormat.Deserialize(stream);//反序列化得到的是一個object物件.必須做下型別轉換
stream.Close();
出錯陳述句,錯誤資訊:System.OutOfMemoryException:“Exception of type 'System.OutOfMemoryException' was thrown.”
請問怎么改呀。。
另外,我突然想到,是不是我123.txt儲存的資料就已經二進制了,不需要再Encoding.ASCII.GetBytes(Str)轉二進制了,試驗了以下用法,
var Str = Properties.Resources.ResourceManager.GetObject("CardPic");
byte[] array =(byte[])Str;
MemoryStream stream = new MemoryStream(array);
BinaryFormatter bFormat = new BinaryFormatter();
CardList = (List<Tuple<string, Bitmap>>)bFormat.Deserialize(stream);//反序列化得到的是一個object物件.必須做下型別轉換
stream.Close();不行!
uj5u.com熱心網友回復:
1. 既然保存成檔案了,為什么不直接使用圖片檔案?2. 圖片也可以直接添加到資源中
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/70695.html
標籤:C#
上一篇:c#求助問題
