tags:

views:

75

answers:

1

Hi, I have a bitmap object which is taking a huge part of the memory at runtime, I want to compress it (JPEG format) in memory then later on use it. I am using this for the compression:

MemoryStream ms = new MemoryStream();
oBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
oBmp.Dispose();
oBmp = null;
Image ResultImg = Image.FromStream(ms);
ms.Dispose();
ms = null;

I dont know if this is really saving some memory or that everything is back to normal memory consumption when I load back the Image from the stream.

anyway I am later on trying to get a byte array from this Image, i am using:

MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();

Where ImageIn is the same Image saved in the previous code. but I am getting a GDI+ exception : [A generic error occurred in GDI+.]

The same code works fine if I didnt do this "in memory compression" but I really need it to save memory.

Thanks

A: 

My Mistake, I should never have called:

ms.Dispose();
TB