无法将类型 'System.String' 的对象强制转换为类型"System.Byte[]"。发布后出错



我将图像保存在数据库中。当我调试它时,它工作得很好。,但是在发布它之后,我尝试加载或尝试保存图片,它给了我类型转换错误。我使用以下代码来保存图像:

MemoryStream ms = new MemoryStream();
kephelye.Image.Save(ms, kephelye.Image.RawFormat);
byte[] img = ms.ToArray();

and load image:

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;
}

相关内容

最新更新