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();
}
}
}