從SQL CE db加載圖像,然後嘗試將其加載到PictureBox中。在.Net中加載圖像時發生OutOfMemory異常
我保存圖像這樣的:
if (ofd.ShowDialog() == DialogResult.OK)
{
picArtwork.ImageLocation = ofd.FileName;
using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open))
{
byte[] imageAsBytes = new byte[fs.Length];
fs.Read(imageAsBytes, 0, imageAsBytes.Length);
thisItem.Artwork = imageAsBytes;
fs.Close();
}
}
,然後使用LINQ to SQL保存到數據庫中。
我加載圖像回像這樣:
using (FileStream fs = new FileStream(@"C:\Temp\img.jpg", FileMode.CreateNew ,FileAccess.Write))
{
byte[] img = (byte[])encoding.GetBytes(ThisFilm.Artwork.ToString());
fs.Write(img, 0, img.Length);
}
,但我得到一個OutOfMemoryException。我已經讀過,這是一個輕微的紅色鯡魚,並可能有文件類型有問題,但我不能確定什麼。
任何想法?
謝謝 picArtwork.Image = System.Drawing.Bitmap.FromFile(@「C:\ Temp \ img.jpg」);
這是什麼類型的藝術品?您將在第一個代碼塊中爲其分配字節,但在第二個代碼塊中將Artwork ToString轉換爲字符,然後將其轉換爲字節。爲什麼多次轉換? – dthorpe 2010-04-19 20:12:28