Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with large bitmaps causes Out of Memory Exception

I need to edit(Increase the height) the Image on the fly. The file is mostly 5000*4000 in dimension. I see the memory shoots up to peak level when I create a bmp of large dimensions and call Graphics.DrawImage method on the bmp instance.

How do I get rid of the Out Of Memory exception? Is there a way to work with large bitmaps in c# ?

like image 538
Ulab Avatar asked Jul 11 '11 08:07

Ulab


People also ask

How do I stop out of memory exception?

To prevent the OutOfMemoryException exceptions, you must modify your application so that less data is resident in memory, or the data is divided into segments that require smaller memory allocations.

How is a bitmap stored in memory?

The bitmap is stored in its original compressed form to save memory (PNG, JPEG, etc) The bitmap is stored in 24-bpp format so its slower to access than a 32-bpp image. The bitmap is not stored in a packed memory array and is fragmented so can't be read/written fast.


1 Answers

The problem is the Huge amount of Memory required for the operation. Yours is taking about some GigaBytes, so the solution could be to use a Stream and process the file in chunks.

Or the the best option would be to use some Third party library for it. Below are some for .Net

AForge

Image Resizer

Also have a look at this SO question.

https://stackoverflow.com/questions/158756/what-is-the-best-image-manipulation-library

like image 171
Shekhar_Pro Avatar answered Sep 20 '22 17:09

Shekhar_Pro