Hi,
I am trying to convert an image to a byte array. But I have searched the internet and most pretty much say to do something similar, which is the code I have below.
But when I call this I get a "generic GDI+ error has occured" exception on the imageIn.Save line, and I don't know why and many search have failed to reveal the problem.
I have tried different images, different machines, but I get the same problem. Can somebody help?
private byte[] byteImageConvert(System.Drawing.Image imageIn)
{
ImageCodecInfo codec = null;
foreach (ImageCodecInfo e in ImageCodecInfo.GetImageEncoders())
{
if (e.MimeType == "image/jpeg")
{
codec = e;
break;
}
}
using (EncoderParameters ep = new EncoderParameters())
{
ep.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, codec, ep);
return ms.ToArray();
}
}
}
Loading
dj_jay_smithPosted Nov 28, 2007, 6:50 AM
private byte[] byteImageConvert(System.Drawing.Image imageIn)
{
Bitmap bmpIn = new Bitmap(imageIn);
MemoryStream msIn = new MemoryStream();
bmpIn.Save(msIn,System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0], null);
return(msIn.ToArray());
}
Mahesh ChandPosted Nov 27, 2007, 8:04 AM
"Generic GDI+ error" occurs when you are doing some wrong operations such as drawing on a surface when there is is no surface. I would make sure that image you are saving is valid and you have correct permissions. Also, make sure operations (encoding) you are doing are valid. Just some pointers.