[英]Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish
I am saving Image in a database. 我正在将图像保存在数据库中。 It works great when I Debug it. 当我调试它的时候效果很好。 , but after publish it and I try to load or try to save the picture it gives me type cast error. ,但是在发布之后,我尝试加载或尝试保存图片,这给了我键入强制错误。 I am using following code to save image: 我正在使用以下代码保存图像:
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();
Simply get the image from the memory stream into a new bitmap, and close the stream. 只需将图像从内存流中获取到新的位图中,然后关闭流即可。
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.