簡體   English   中英

無法將類型為“ System.String”的對象轉換為類型為“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM