I have a JPEG stored as a byte array, and I would like to convert it to a System.Drawing.Image, as shown below:
using(var ms = new MemoryStream(myArray))
{
img = System.Drawing.Image.FromStream(ms);
Console.WriteLine(ImageToByteArray(img).Length);
}
However, the problem that I'm running into is that I'm losing and my image is degrading. My initial array is 2673030 bytes long, as is the MemoryStream, but when I look at the length of the img, it is only 2672013 bytes long. What's even stranger is that when I run this snippet on our docker container, the image is shrunk down all the way to 1700324 bytes. These sizes are from the print statement shown, so it doesn't have to do with the file format.
I've tried reading from the stream as a BitMap, and I've also tried writing the byte array to a file and creating the image/bitmap directly from the file, all with the exact same resize results.
The heart of the problem is that my images are losing quality when I save them to the server. As far as I can tell, the only difference is that FromStream() returns differently sized images, so does anyone know of a) a way to create my image without losing data, and b) why it is getting resized differently on different machines? A) is a much higher priority answer.
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
}