Here's an intriguing little problem....
I want to load some BMP bitmaps as checkmarks in a menu.
Ok, the app can use LoadBitmap() or LoadImage() to get it from
the resource section or from file.

But, what if the bitmaps are already in memory? That is, the files have
been opened and the bitmaps copied into memory (not necessarily in separate
memory blocks, maybe inside a single block created by GlobalAlloc()).
How on earth do you get a handle to a bitmap image already in memory?

I know of two solutions, but neither seem elegant...

Write the bitmap to a temp file, use LoadImage() to get it, with handle.
-- messy!

2. Donkey wrote in a thread Feb 23, 2003:

If you want to move a bitmap from memory into your resource section you can use UpdateResource:

invoke UpdateResource,hFile,RT_BITMAP,lpResName,wLanguage

You should note that lpData point to the data to be stored in the resource file, it only points to the raw data, a handle will not do. You have to convert the handle to a memory position then use that to point to the data.

I haven't tried either, but surely there is some simple elegant way to get a
handle to the bitmap, if you have the address of the bitmap,
then use that handle to upload to the menu item.
In my case I will use SetMenuItemBitmaps() to upload the checkmarks.
Does LoadImage() just create a simple memory image of the
file, or does it put in other stuff? -- if the latter, then the problem should be
easy to solve.

This is an interesting little problem.
I wonder what the relationship is between handle and address...

Barry Kauler
Posted on 2003-03-12 17:52:09 by bkauler
I found a solution of sorts:

...umm, that's a mighty long URL.
Has code to read BMP to memory and create a DIB. Can later get a handle to it
whenever required.

Anyway, I was wondering. Say I used LoadImage() to load my bitmaps into
memory. This func supplies handles.
Would it be possible to get a pointer to the loaded image, then copy the image
to where I want to save it. The requirement is at a later time want to get a handle of that copy...

Barry Kauler
P.S. will be away for a couple of days, so can't respond immediately to any postings.
Posted on 2003-03-12 18:31:34 by bkauler
Oh, the URL got truncated.
Go to Google Groups and search on subject "Bitmaps in memory"
it was in group :

Barry Kauler
Posted on 2003-03-12 18:33:56 by bkauler
I've tried this, it works:

;open a temp file for loading of the bmp checkmarks... v3.45
invoke CreateFile,ADDR sztempbmp,GENERIC_READ OR GENERIC_WRITE, \
mov htempfile,eax

;this code is in a loop, loading bitmaps to menuitems...
mov eax,(OBJECT PTR ).ximage ;handle of bitmap image.
.IF eax==0 ;bmp not loaded.
;we have bmp file in library-data-element.sztext+128, need to call
;LoadImage(), so need to write to a temp file 1st..
invoke SetFilePointer,htempfile,0,0,FILE_BEGIN
invoke SetEndOfFile,htempfile
mov eax,(OBJECT PTR ).nshape ;size of file.
lea ebx,(OBJECT PTR ).sztext
add ebx,128 ;address of file in memory.
invoke WriteFile,htempfile,ebx,eax,ADDR nbyteswrote,0
invoke LoadImage,0,ADDR sztempbmp,IMAGE_BITMAP,0,0, \
mov (OBJECT PTR ).ximage,eax ;handle of loaded bitmap.
;now load the checkmark to the menuitem...
.IF eax != 0
invoke SetMenuItemBitmaps,helementsmenu,menuitemid,MF_BYCOMMAND,eax,eax

invoke CloseHandle,htempfile ;close temp bmp file. v3.45
invoke DeleteFile,ADDR sztempbmp

I've got all these BMP files, tiny 12x12 mono files (110 bytes) read into memory,
and they have to be loaded as checkmark bitmaps to the menuitems.

I wondered if it would be possible to save to a temp file, as I've done above,
use LoadImage() and when finished delete the file, without Windows cache
management even writing to physical disk -- no such luck though, its not that

This method is not very satisfactory, because is inherently slow.
I'll look at what Ernie has done. It has to become a "bitmap" compatible
with SetMenuItemBitmaps().

Incidentally, I'm using this "checkmark" function not for displaying checkmarks
as such, but as a simple method of displaying a picture before each menu item,
rather than going for owner-draw.
Limitation is the bitmap must be small and monochrome.

Barry Kauler
Posted on 2003-03-14 05:47:50 by bkauler

But, what if the bitmaps are already in memory?

Do you mean bitmap is already loaded but you want to
use as gfx object?
in BITMAP struct fill size and pointer to memory of bits of loaded BMP.
Posted on 2003-03-14 06:44:40 by The Svin
The bitmap file is read as-is from memory, later I want to convert it to a
GDI bitmap handle.
The method I have given, write back out to file, then use LoadImage()
works but not elegant.

The light suddenly came on. I have previously used OleLoadPicture()
to load and display BMP, GIF and JPG files (see my website ).
However I had this vague memory, and checked it out -- there is a method
of the IPicture interface called get_Handle(), that gets a GDI bitmap handle
from a IPicture.

Don't worry if that reads like gobbledy-gook, and you haven't got a clue about
COM. I have placed the code in the following file:

You don't need any COM include files or anything to use the code.

There is a document that I once read on the MSDN site, that did the same
thing. Ernie has done similar stuff also.

Barry Kauler
Posted on 2003-03-15 07:51:14 by bkauler