How can I draw a large bitmap from several smaller bitmaps without running into memory issues?

35 Views Asked by At

I’m trying to composite several small bitmaps into one large 20k X 12k pixel, 24 bit bitmap.

For now, I am creating the large bitmap in one go by using the Bitmap class constructor that accepts a width and height. Of course, this loads the whole thing into memory.

One smaller bitmap is then created and written to the larger bitmap using Graphics.DrawImage. The small bitmap is then disposed immediately to conserve memory and the next small bitmap is loaded and drawn, so on and so on.

After all small bitmaps are composited to the large bitmap, the large bitmap is saved to disk as a .png using Image.Save.

Intermittently (about 10% of the time) I will receive an “Argument exception: parameter is invalid.”

To the best of my research, this is GDI+’s round about way of saying it can’t allocate a large enough contiguous memory block for the image.

My question is, how can I accomplish this without running into memory issues? I am thinking a memory mapped file may be the answer, but I have never used them before and I fear I’d have to write my own code for “drawing” each small bitmap and also for exporting as a PNG.

My apologies for lack of code. I’m about to board a plane and don’t have access to it at the moment.

0

There are 0 best solutions below