0

I have a image field in sqlserver.

When insert row to Database i convert myimage to byte whith this code.

 Image img = Image.FromFile("D:\\Test.PNG");
 ImageConverter converter = new ImageConverter();
 enter.Pic = (byte[])converter.ConvertTo(img, typeof(byte[]));

When i want read this record, i convert byte to image with this cod e:

  ImageConverter ic = new ImageConverter();
  Image img = (Image)ic.ConvertFrom(enter.Pic);

but when convert byte to image, i get error

Parameter is not valid

4

1 回答 1

2

要将图像转换为字节数组,您可以直接使用以下File.ReadAllBytes方法:

enter.Pic = File.ReadAllBytes(@"D:\Test.PNG");

并将字节数组转换回Image实例:

using (Stream stream = new MemoryStream(enter.Pic))
using (Image image = Image.FromStream(stream))
{
    // use the image here
}
于 2012-12-19T06:42:03.623 回答