I'm trying to draw a bitmap file on window. So I used (HBITMAP)LoadImage() and it returns NULL. I used GetLastError to see the problem but it returns 0 too. I'm working on goorm ide(windows application).
HBITMAP hBitmap = NULL;
hBitmap = (HBITMAP)LoadImageW( NULL, L"C:\\Users\\Asd\\Downloads\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
if(hBitmap == NULL){
DWORD errorCode = GetLastError();
if(errorCode != 0){
LPSTR messageBuffer = nullptr;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
0,
(LPTSTR)&messageBuffer,
0,
NULL);
MessageBox(NULL, messageBuffer, "hBitmap is NULL!" , MB_OK);
}else{
MessageBox(NULL, "hBitmap is null but errorCode is 0", "???" , MB_OK);
}
}
If I change the "image.bmp" to "asdf.bmp" in the code, It says "there is no file." so I'm sure that It found file but seems not working right. when I print Width and Height of bitmap, It shows strange number(like 12312321, -3453453). I can't find what is problem.
I tried this too: (from https://support.microsoft.com/en-us/help/158898/howto-how-to-use-loadimage-to-read-a-bmp-file)
hBitmap = (HBITMAP)LoadImage( NULL, "image.bmp", IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
HBITMAP
is already a pointer type, usingHBITMAP*
is wrong. And a typecast can't produce a NULL unless the value being casted is already NULL to begin with. - Remy Lebeau.bmp
file? You are currently using a relative path. Also see LoadImage() returns NULL and GetLastError() returns 0. One thing I notice is that yourWM_PAINT
code isn't doing any error handling, and it destroyshBitmap
after the 1st paint, leavinghBitmap
invalid for subsequent paints. Since you loadhBitmap
inWM_CREATE
, you should be destroying it inWM_DESTROY
instead. - Remy Lebeau.bmp
file itself. Have your tried using other.bmp
files? - Remy Lebeau