I'm having a problem with converting raw bytes which are stored in a specific format in a file to a Bitmap. The Bitmap gets created with no errors but it isn't being displayed. I tried a normal bitmap and it works perfectly

Firstly heres the working code with loading a normal bitmap.

char *file = "apple.bmp";

bmp = LoadImage(0, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
/* ImageBox is a handle to a static image control */
SendMessage(ImageBox, STM_SETIMAGE, IMAGE_BITMAP, bmp);

This works perfectly.

Now for the raw bytes

unsigned short bleh[44*44];
/* Get the bytes from a file */

bmp = CreateBitmap(44, 44, 3, 5, &bleh);
SendMessage(ImageBox, STM_SETIMAGE, IMAGE_BITMAP, bmp);

I can't seem to get this to work, it seems to create the bitmap (no error) but not being displayed.

I also tried
SetBitmapBits(bmp, 44*44*2, &bleh);

but not success either.

Can anyone give me a hand please? Sorry its in C and not asm though, but this is only site I could find on Win32 API.

I also tried browsing the forums (doing a search) and there was a CreateCompatableBitmap() option but this is not what I need because I'm not dealing with Memory Devices.

Also the raw image format is size 44 x 44 15 bit RGB
Posted on 2004-08-14 07:57:31 by rorra
3 color planes and 5 bpp ???

You might consider using 32 bpp it is much easier to work with. And only one color plane, it has nothing to do with the bits per color. Also the scan lines, not the pixels, need to be WORD aligned, at 15 bpp that's a bit tough I think it works out to 41 WORD values+4 bits. Remember also that colors are reversed and shifted in raw pixel format (BSWAP and SHR 8) so a pixel with a color value of 00AABBCC would be 00CCBBAA in raw format....

bitmapbits DD 44*44 DUP (008080FFh)

invoke CreateBitmap,44,44,1,32,offset bitmapbits
invoke SendDlgItemMessage, [hwnd], 1001, STM_SETIMAGE, IMAGE_BITMAP, eax

Will create a bluish bitmap. You can substitute the 008080FFh for an array of color values, remembering that it is in the format 0RGB not the 0BGR that you are used to.
Posted on 2004-08-14 08:49:55 by donkey
Thanks very much. Converted it to 32 bit and it worked perfectly!
Posted on 2004-08-14 09:31:44 by rorra
Ok I've run into another problem.
I have a listbox of numbers from 0-65535 and I have a Static Image Box used to display an image. When I select an entry in the listbox it looks up the number and loads the bitmap from file. I'm explictly deleting the Bitmap everytime I load a new one into the imagebox but while I'm scrolling through the listbox the application gathers up memory like crazy. It can gain up to 30 megs (and it starts off at only 300kb). The really strange thing is when I minimise the application the entire memory that was gathered is deallocated and it goes back down to 300kb memory consumption. When I maximise it and begin scrolling it gathers up again (starting at 300kb). I was wondering if this was a Windows problem or is it me? I know I'm deallocating all the data when I'm loading etc. and I'm nearly sure it has something to do with the display otherwise windows wouldn't deallocate it.

Heres my little code segment that deletes:

/* This just creates the imagebox */

HWND CreateImageBox(DWORD x, DWORD y, DWORD nWidth, DWORD nHeight, HWND hWndParent,
HMENU hMenu)
return CreateWindowEx(0, "STATIC", 0, style, x, y, nWidth, nHeight,
hWndParent, hMenu, (HINSTANCE)hWndParent, NULL);

/* Creates the listbox */
HWND CreateListBox(DWORD x, DWORD y, DWORD nWidth, DWORD nHeight, HWND hWndParent,
HMENU hMenu)
return CreateWindowEx(WS_EX_CLIENTEDGE, "LISTBOX", 0, style, x, y, nWidth, nHeight,
hWndParent, hMenu, (HINSTANCE)hWndParent, NULL);

/* Heres the deleting/allocating */

if (LOWORD(wParam) == IDC_LISTBOX)
index = SendMessage (hwndList, LB_GETCURSEL, 0, 0);
length = SendMessage (hwndList, LB_GETTEXTLEN, index, 0) + 1;
pEntry = calloc (length, sizeof (TCHAR)) ;
SendMessage (hwndList, LB_GETTEXT, index, (LPARAM)pEntry);

bmp = SendMessage(hImageBox, STM_GETIMAGE, IMAGE_BITMAP, 0);
if (bmp)
bmp = /* function to get the bitmap from file */
bmp = SendMessage(hImageBox, STM_SETIMAGE, IMAGE_BITMAP, bmp);

Thanks again.
Posted on 2004-08-16 09:49:00 by rorra
Hm, try checking the return value from DeleteObject to see if the bitmap is really deleted - perhaps it can't be deleted while it is selected into the static control?
Posted on 2004-08-16 10:02:36 by f0dder
You are correct it wasn't deleting it properly. I tried making two Bitmap handles to store the old bitmap with GETIMAGE, setting the image static box to the new bitmap then deleting the old, however it still isn't getting deleted. Perhaps the new bitmap needs to be written to the Static box first before it released the old one? Is there any event I can use that will tell me that the window has finished redrawing?

bmpold = SendMessage(hImageBox, STM_GETIMAGE, IMAGE_BITMAP, 0);

bmp = /* read bitmap function */
bmp = SendMessage(hImageBox, STM_SETIMAGE, IMAGE_BITMAP, bmp);
if (bmpold)
if (DeleteObject(bmpold) != 0)
Posted on 2004-08-16 12:32:29 by rorra
Try STM_SETIMAGE with 0 instead of bmp parameter?
Posted on 2004-08-16 12:36:57 by f0dder
Tried that also, the DeleteObject() won't work at all, it keeps returning non zero which means the resource wasn't deleted! Windows must be locking it somehow.
Posted on 2004-08-16 13:13:07 by rorra
Hm, try DeleteObject without STM_SETIMAGE, to see if you can delete the bitmap at all? If that works, perhaps STM_GETIMAGE before storing any image, and obj=STM_GETIMAGE, STM_SETIMAGE(original), DeleteObject(obj) ?
Posted on 2004-08-16 13:23:57 by f0dder
I don't know, once the bitmap is returned from my function that creates it, it won't even let me delete it. I'll have to look into it see whats the problem. Thanks for the effort.
Posted on 2004-08-16 13:55:28 by rorra