开发者

Image in memory compression exception when getting byte array from the Image

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 compressio开发者_StackOverflown:

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


My Mistake, I should never have called:

ms.Dispose();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜