views:

110

answers:

5

Is there any way to create a more space/resource efficient bitmap? Currently I try to render a file, approx 800px high but around 720000px wide. It crashes my application, presumably because of the share memory-size of the bitmap.

Can I do it more efficient, like creating it as an gif directly and not later when I save it? I try to save a series of lines/rectangles from a real world reading, and I want it to be 1px per 1/100th of a second.

+3  A: 

You're going to have to either:

  • Force x64 environment and get a stack load of RAM.

  • Change your architecture

Your image is going to be a little over 2Gb.

Rowland Shaw
I think You meant 2GB
Tomek Tarczynski
Even switching to x64 will not solve the issue immediately. The maximum object size in the GC Heap is still at 2GB, even in the x64 version of the .NET 2.0 runtime.
0xA3
+12  A: 

You have to remember that any image you load into memory regardless if it's a GIF or JPEG or whatever on disk will be turned into a 32 bit bitmap which means four bytes per pixel.

This means that the image you're creating will be:

4 bytes * 800 pixels high * 720,000 pixels wide = 2,304,000,000 bytes

You're basically blowing your memory by trying to create an image that large.

For whatever you're trying to accomplish the answer is tiling and caching your image.

Paul Sasik
A: 

Your image is about 2.3 gig, and the biggest .Net object you can have is 2 gig regardless if the machine is 32 or 64 bit.

You're going to have to break the bitmap up in chunks to handle an image that size.

Kevin
A: 

Can i do it more efficient, like creating it as an gif directly and not later when i save it?

You can compress the image as you write it. It won't be in (uncompressed/unencoded) "bitmap" format anymore. Examples of compression algorithm include "run-length encoding" and "huffman".

Also, use the least possible color-depth: preferaby black-and-white, i.e. 1-bit-per-pixel.

Also perhaps save it in several, smaller, discontinuous memory chunks: instead of a single huge memory chunk (so huge that it can't even be allocated in the first place).

ChrisW
A: 

If you create it as 720000px high and 800px wide as a bmp and rotate it when actually displaying it(*), you can stream the data directly to file in bitmap form. Maybe use RLE instead of raw bitmap; streaming in this manner should still be possible in that case.

(*)Displaying it is left as an exercise to the reader. You'll need tiling or something.

Brian