[英]Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.”
[英]Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish
我正在將圖像保存在數據庫中。 當我調試它的時候效果很好。 ,但是在發布之后,我嘗試加載或嘗試保存圖片,這給了我鍵入強制錯誤。 我正在使用以下代碼保存圖像:
MemoryStream ms = new MemoryStream();
kephelye.Image.Save(ms, kephelye.Image.RawFormat);
byte[] img = ms.ToArray();
並加載圖片:
DataSet ds = new DataSet();
da.Fill(ds);
byte[] ap = (byte[])(ds.Tables[0].Rows[0]["kepcim"]);
MemoryStream ms = new MemoryStream(ap);
if (ms.Length != 0) {
kephelye2.Image = Image.FromStream(ms);
}
ms.Close();
只需將圖像從內存流中獲取到新的位圖中,然后關閉流即可。
public static Image GetImage(byte[] buffer, int offset, int count)
{
var memoryStream = new MemoryStream(buffer, offset, count);
Bitmap bm = new Bitmap(Image.FromStream(memoryStream));
// Close the stream here, won't affect the bitmap.
return bm;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.