Bitmap Issue

Everytime I run my program LoadBitmap () returns NULL, I read a few things on google and I still don't know why.

Window procedure (Only things that have to do with the .bmp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HBITMAP hbBitMap = NULL;
    switch (msg)
    {
		case WM_CREATE:
        hbBitMap = LoadBitmap (GetModuleHandle (NULL),
                               MAKEINTRESOURCE (BITMAP_PICTURE));
        if (hbBitMap == NULL)
        {
            MessageBox (hwnd,
                        "Bitmap failed to load",
                        NULL,
                        MB_OK | MB_ICONWARNING);
            DestroyWindow (hwnd);
        }
        break;

        case WM_PAINT:
        {
            BITMAP bm;
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint (hwnd, &ps);
            HDC hdcMem = CreateCompatibleDC (hdc);
            HBITMAP hbmOld = (HBITMAP) SelectObject (hdcMem, hbBitMap);
            GetObject (hbBitMap, sizeof (bm), &bm);
            BitBlt (hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
            SelectObject (hdcMem, hbmOld);
            DeleteDC (hdcMem);
            EndPaint (hwnd, &ps);
        }
        break;
    }
    return DefWindowProc (hwnd, msg, wParam, lParam);
}


Defines.h
1
2
3
4
#define ID_MENU 0x7000
#define IDM_EXIT 0x7001
#define IDM_FULL_SCREEN 0x7002
#define BITMAP_PICTURE 0x7003 


Resource.rc
1
2
3
4
5
6
7
8
9
10
11
12
#include "defines.h"

BITMAP_PICTURE BITMAP "Bitmap.bmp"

ID_MENU MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&Full Screen", IDM_FULL_SCREEN
        MENUITEM "&Exit", IDM_EXIT
    END
END


I think I have the .bmp in the right place, if I move it CodeBlocks says that there is no such image as "Image.bmp" or something like that.

Any help? Thanks
Last edited on
Hi

got your code working here with only one change

 
 HBITMAP hbBitMap = NULL;


needs to be done outside of windowProc because it is a local variable and every time
that window proc is called it is reset.

The only other thing is to make sure that the bitmap is in the same directory as your
.rc file when it is compiled.

Hope this was helpful
Shredded
I put HBITMAP hbBitMap = NULL; in global scope and the same thing happens. And yes my bitmap is in the same directory as my resource.
Hi again.

just guessing here but I got it to fail by removing the .res file from the makefile.
(it compiles and links without error)

Your using codeblocks and i am unsure of where to find it but make sure that the result from
compiling your rc file is been linked into the program.

other than that I am out of ideas
Shredded
I've never used a makefile for windows programming before, I think C::B does it for me or something =/

And I went to Project > Include Files > and selected by resource, if that is what you mean.

:S
Hi,
Yeah i normally compile from the command line.

anyway i installed codeblocks to test it out and it compiled and worked.

i did have to add the rc file under Projects

Workspace
     test ->
         Sources
              bmp.cpp
         Resources
              bmp.rc


but I assume that you have already done that.

Sorry I cant be of more help
Shredded
You should generally try to avoid going into the global scope. Instead, use:

static HBITMAP hbBitMap = NULL;

and you can keep it inside WinProc!
Last edited on
Try including windows.h in your .rc file.

Also I think its better to load your bitmap locally in the WM_PAINT function each time, and delete it at the end of the wm_paint with DeleteObject(bmp); then you wont be holding it in memory all the time. If you are going to load in it wm_create, then you better make sure you delete it in wm_destroy.
Last edited on
Sorry for the late reply, but I've been putting this project on hold for a little bit. Today I started to work on this again. I tried a few things, read over this thread, used google for 45 minutes, and still nothing. Here is the CodeBlocks project that has everything that I've been working on:
http://www.sendspace.com/file/i0dm2x
It would be great if someone could download it and take a look at it PLEASE.

Thanks so much to everyone who already helped and everyone who will help !
Last edited on
Anyone? I really need help =/
Hi again,

I tried your code and it appears that it is the bitmap file itself.
I tried other files and it worked.

Then I opened your test.bmp in paint and resaved it and guess what it worked.

I Just made one change to your functions.h file which was

1
2
3
4
5
6
        hbBitMap = LoadBitmap (GetModuleHandle (NULL), "Test.bmp"); 

        // changed to

        hbBitMap = (HBITMAP) LoadImage (NULL, "test.bmp",IMAGE_BITMAP,
							0,0,LR_LOADFROMFILE);


Because you are no longer trying to load from resource

You will also need to place the .bmp in the same folder as the executable.

Hopefully this solves your problem
Shredded.
cool thank you so much :D
Topic archived. No new replies allowed.