Re: Creating lots of bitmaps

Tech-Archive recommends: Fix windows errors by optimizing your registry



Awesome.
Thanks for your reply.
Nathan

John Carson wrote:
"Nathan" <nathan@xxxxxxxxx> wrote in message
news:%23jl$w5moGHA.220@xxxxxxxxxxxxxxxxxxxx
I have a program that creates tons of bitmaps using CreateDIBSection.
What is happening is it is causing a lots and lots of page faults and
it is totally fragmenting memory.
Is there anyway around this?

Is it possible to allocate myself the memory that CreateDIBSection or
CreateDIBitmap uses? That way I can allocate a huge chunk of memory
that can be used for a bitmap of any size.

And then use another function to give the HBITMAP the new width and
height dimensions each time
a new image size is required?


I take it that you don't need "tons" of bitmaps simultaneously, but that a succession of them are created and destroyed.

CreateDIBSection allows you to pass the handle to a memory mapped file that is used to store the pixel bits. You can re-use that file for a new bitmap when it is no longer needed for an old bitmap.

You can do it as follows:

HANDLE hfm = CreateFileMapping(INVALID_HANDLE_VALUE,
0, PAGE_READWRITE,
0, 2048 * 2048 * 4,
_T("BitmapStorageFile"));

The first argument of INVALID_HANDLE_VALUE means that the system will create a file for you rather than use one that you provide. The second last argument (2048 * 2048 * 4 in the example above) is the number of bytes you want to reserve for the pixel bits.

You then simply call CreateDIBSection as usual, passing hfm (the handle returned by CreateFileMapping) as the second last argument.

After you delete the DIB section bitmap, you can call CreateDIBSection again and pass it the same hfm handle. The data for the first bitmap stored by the memory mapped file file will be overwritten with the new data.

.



Relevant Pages

  • Re: Writing to 8-bit memory bitmap
    ... ppvBits is a pointer to the memory that was allocated with CreateDIBSection. ... You may cache this pointer and use it later. ... for the bitmap ... CreateDIBSection allocates memory using file mapping. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Writing to 8-bit memory bitmap
    ... but the CreateDIBSection documentation is confusing. ... hSection...if hSection is NULL, the system allocates memory for the DIB. ... This seems to suggest that you cannot get hold of the memory that is used for the bitmap, ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Creating lots of bitmaps
    ... it is totally fragmenting memory. ... that can be used for a bitmap of any size. ... CreateDIBSection allows you to pass the handle to a memory mapped file that ... memory mapped file file will be overwritten with the new data. ...
    (microsoft.public.win32.programmer.gdi)
  • CreateDIBSection and non-memory device contexts
    ... Every code example of CreateDIBSection I have looked at selects the ... bitmap into a memory DC and then bitblts from the memory dc to the ... there anything wrong with selecting a bitmap into a screen DC ... the function uses this device context's logical palette to initialize ...
    (microsoft.public.win32.programmer.gdi)
  • Re: memory leak
    ... ArgumentException on Full Framework) when I have a ton of virtual memory ... "new Bitmap" line. ... Dispose is not a required call - it is optional. ... you are done with those resources. ...
    (microsoft.public.dotnet.framework.compactframework)