【发布时间】:2015-11-09 21:39:53
【问题描述】:
到现在为止,这是我从数据库中将图像放入 picturebox1 所做的,但现在我收到了这个错误:
无法将
System.DBNull类型的对象转换为System.Byte[]类型。
该平台已有的解决方案不起作用。
Image img;
byte[] bytimg = (byte[])dt.Rows[0]["Picture"];
//convert byte of imagedate to Image format
using (MemoryStream ms = new MemoryStream(bytimg, 0, bytimg.Length))
{
ms.Write(bytimg, 0, bytimg.Length);
img = Image.FromStream(ms, true);
pictureBox1.Image = img;
}
【问题讨论】:
-
先检查
dt.Rows[0]["Picture"]是否为空。 -
考虑在此处也检查此答案:stackoverflow.com/questions/870697/…
-
错误很明显。您不能将空值强制转换为字节数组。在尝试强制转换之前检查该值是否为空。
-
检查
if(dt.Rows[0]["Picture"] == null){ return} else你需要将 using 包裹在 Else 周围,否则你仍然会得到错误