简体   繁体   English

无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误

[英]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.

相关问题 Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将System.string强制转换为System.Byte [] - Unable to cast System.string to System.Byte[] 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32' 错误-无法将类型为“ System.Byte []”的对象转换为类型为“ System.IConvertible”的对象 - Error-Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM