开发者

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

开发者 https://www.devze.com 2022-12-15 04:07 出处:网络
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

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

精彩评论

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