6
votes

I'm working on some university project and got stuck with memory issue. I load a bitmap which takes about 1,5GB on HDD with code below:

Bitmap bmp = new Bitmap(pathToFile); 

The issue is that the newly created Bitmap object uses about 3,5GB of RAM which is something I can't understand (that's really BIG wrapper :E). I need to get to the pixel array, and the use of Bitmap class is really helpful (I use LockBits() method later, and process the array byte per byte) but in this case it's total blocker. So here is my question:

Is there any easy way to extract the pixel array without lending additional 2gb?

I'm using c# just to extract the needed array, which is later processed in c++ - maybe I can extract all needed data in c++ (but conversion issue appears here - I'm concentrating on 24bgr format)?

PS: I need to keep the whole bitmap in memory so splitting it into parts is no solution.

PS2: Just to clarify some issues: I know the difference between file extension and file format. The loaded file is uncompressed bitmap 3 bytes per pixel of size ~1.42GB (16k x 32k pixels), so why Bitmap object is more than two times bigger? Any decompressing issues and converting into other format aren't taking place.

3
@Jason: Yes, but that question has no actual answer. Don't know why they've overseen memory mapped files.Marcel N.
What encoding does the bitmap use? Is there RLE compression?Ben Voigt
bitmap's coding is 24-bit with no RLE compression so the only thing I want is to deserialize the pixel array :( it's size is 16k X 32k pixels so with 3 bytes per one pixel it takes 1.42GB so I really don't understand why Bitmap object created with that file takes additional 2GB of dataRafal Chmiel
Ignoring the on-disk size, what are the dimensions and bit depth of the image. This will tell you the minumum requirements to hold the image data in memory (multiply them all together - assuming its not a indexed image - and bit depth a s bytes per pixel). This will then tell you if it's compression etc or memory overhead of Bitmap class that is your issue.Wolf5370

3 Answers

1
votes

Consider using Memory Mapped Files to access your HUGE data :). An example focused on what you need can be found here: http://visualstudiomagazine.com/articles/2010/06/23/memory-mapped-files.aspx It's in managed code but you might as well use it from equivalent native code.

Let me know if you need more details.

0
votes

You can stop memory caching.

Instead of

Bitmap bmp = new Bitmap(pathToFile);

Use

var bmp = (Bitmap)Image.FromStream(sourceFileStream, false, false);

see https://stackoverflow.com/a/47424918/887092