简体   繁体   中英

Error: Parameter is not valid, when convert byte to image

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

To convert the image to a byte array you could directly use the File.ReadAllBytes method:

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

And to convert a byte array back to an Image instance:

using (Stream stream = new MemoryStream(enter.Pic))
using (Image image = Image.FromStream(stream))
{
    // use the image here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM