Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large Bitmap on Delphi

I want to create large Bitmap with code

  LargeBmp := TBitmap.Create;
  try
    LargeBmp.Width := 1000; // fine 
    LargeBmp.Height := 15000; // XP - EOutOfResources, Not enough memory, Win 7 - works fine
    ...
  finally
    FreeAndNil(LargeBmp);
  end;

This code raises an EOutOfResources exception with message "Not enough memory" on Windows XP but works fine in Windows 7.

What is wrong? Why Not enough memory? It's only 60 MB.

like image 792
barbaris Avatar asked Oct 25 '12 07:10

barbaris


3 Answers

Set the pixel format like this:

LargeBmp.PixelFormat := pf24Bit;

I had the same problem several times and that always solved it.

like image 129
jpfollenius Avatar answered Sep 29 '22 09:09

jpfollenius


As was discussed already, if you don't set the pixel format Windows will see it as a device-dependent bitmap. When you set the pixel format you create a DIB (device-independent bitmap). This means it's independent of the display device (graphics card).

I've had this same problem, and wanted to point out that pf24bit is not the only option. In the Graphics unit, you also have:

TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);

For a project I'm working on, I found the 8 bit option worked the best for what I needed, since I had a very large bitmap (high resolution), but limited colors (I was creating the whole bitmap from some simple code).

So try out a few others, other than just pf24bit, to find what's optimal for your production environment. I saved quite a bit of internal memory with the pf8bit option.

like image 37
Ben C Avatar answered Sep 29 '22 10:09

Ben C


Created bitmaps (by default) is stored in some buffer. That buffer's size depends on videodriver, os and God knows what else. This buffer can be pretty small (about 20-25mb) and if you will try to create more, it will fail.

To avoid this try to create DIB instead of TBitmap, or try to change Pixelformat to pf24bit. This will tell system to create Bitmap in user's memory instead of GDI buffer.

Now, why it not fails in win7 you ask? Ok, probably cause there is no GDI, but GDI+ and Direct2D uses in win 7 instead. Maybe other driver's version, dunno.

like image 28
Darthman Avatar answered Sep 29 '22 10:09

Darthman