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

笑着哭i 提交于 2019-12-12 01:43:20

问题


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


回答1:


My Mistake, I should never have called:

ms.Dispose();


来源:https://stackoverflow.com/questions/1951813/image-in-memory-compression-exception-when-getting-byte-array-from-the-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!