Loading bitmap file into HBITMAP

I'm trying to load a .bmp file into a HBITMAP using LoadImage but it doesn't seem to work.

1
2
3
HBITMAP b = (HBITMAP)LoadImage(0, "c:\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	if (b)
		MessageBox(0,"loaded", 0, 0);


Is there any other way to do it ?
Another way to do it? Sure.
https://msdn.microsoft.com/en-us/library/windows/desktop/ee719660(v=vs.85).aspx
Easier? Probably not.

Use this function to debug:
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
void ErrorExit(PTSTR lpszFunction) 
//from https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
// Format a readable error message, display a message box, 
// and exit from the application.
{ 
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(1);
}
Is it a 32-bit bitmap? Try LR_CREATEDIBSECTION?
@Homberto
Thanks for the answer. But I'm using VS 2013 Express which doesn't support resource editing so i'm trying to avoid that as much as possible. When i use:

1
2
3
	HBITMAP b = (HBITMAP)LoadImage(0, "image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	if (!b)
		ErrorExit(0);


This is what i get:

(null) failed with error 0: The operation completed successfully.

I thought that maybe it was actually working but when i use:

1
2
3
4
	SIZE bsize = { 0 };
	GetBitmapDimensionEx(b, &bsize);
	std::string dm = std::to_string(bsize.cx);
	MessageBoxA(0, dm.c_str(), 0, 0);


I get 0 as output.

@RaduV That didn't work out sadly.
So, what exactly isn't working? What's the value of b?
To debug variable values do this:
1
2
3
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << b; //#include <iostream> of course 
Hi. Just installed a version of VS which supports resource editing. I was abled to achieve my goal using your link. Thanks very much.
Topic archived. No new replies allowed.