如何转换系统.Byte[]到Image

本文关键字:Byte Image 系统 何转换 转换 | 更新日期: 2023-09-27 18:18:24

嗨,我有以下代码,其中数据变量已填满字节数组从数据库现在,当我想把这些存储在数据库中的字节转换成图像,并希望在图片框中显示它们,它给出了一个错误在

pictureBox1.Image = Image.FromStream(ms);

,错误是参数不合法。我应该做的是以下是我的代码:

byte[] data = (byte[])ds.Tables[0].Rows[i][7];
MemoryStream ms = new MemoryStream(data);
ms.Write(data, 0, data.Length);
ms.Position = 0;
//Image img= Image.FromStream(ms);
pictureBox1.Image = Image.FromStream(ms);

如何转换系统.Byte[]到Image

你的代码看起来很好;可能是您保存的图像格式不正确。这可能也有帮助:http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx

此外,尝试将CanWrite属性设置为True:

MemoryStream ms = new MemoryStream(data,true);编辑:

正如你所问的:数据类型应该是Image,我建议总是使用PNG格式,而不是GIF。