繁体   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