c#将二进制 转换为图片的问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:40:45
我写了这样的代码:
public Image BytesToImage(byte[] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
Image image = new Bitmap(ms,true);
return image;
}
缺在Image image = new Bitmap(ms,true);中报错说 使用无效参数 这是为什么
bytes是用sql 语句
byte[] bytes=(byte[])reader["photo"];
这个有错吗?

我可以肯定的说你的函数写的没问题,因为我试过了。

FileStream FS = new FileStream("未命名.JPG", FileMode.Open);
BinaryReader BR = new BinaryReader(FS);
byte[] bytes = BR.ReadBytes((Int32)FS.Length);
Image image1 = BytesToImage(bytes);
image1.Save("未命名1.JPG", System.Drawing.Imaging.ImageFormat.Jpeg);

结果是生成了一张和未命名.JPG一样的图片。所以的函数写的没有问题,问题在于你传入的bytes并不是一张图片的二进制数组。

"photo"字段存放的是图片的二进制信息吗?你可以调试一下,看看reader["photo"]里面到底是什么内容。

stream 不包含图像数据,或者为 空引用(在 Visual Basic 中为 Nothing)。

- 或 -

stream 包含一个 PNG 图像文件,其单维大小大于 65,535 像素。

stream 不包含图像数据,或者为 空引用(在 Visual Basic 中为 Nothing)。

你传入的 bytes 是否是有效的图片数据啊?