1
votes

I'm having issues displaying a bitmap on the screen. When I complile and run its not displaying anything so I tried debugging step by step and found that the below code is the issue.

HBITMAP hbm = (HBITMAP)LoadImage(hInstance, 
                        "C:\\Users\\Jemma\\Desktop\\Maze Game\\Assets\\TILE_01.bmp", 
                        IMAGE_BITMAP, 
                        SWidth, SHeight, 
                        LR_LOADFROMFILE | LR_CREATEDIBSECTION); 
    if (hbm == NULL || m_Surface == NULL)
{
    DWORD lastError = GetLastError();
    return 1;
}

When I get to this function under autos first it says hbm=0xcccccccc{unused=???} (I'm assuming it's due to not being initialised as this comes up when I get to the line so I haven't stepped into it at this point - Just thought I'd put it in just incase) after I've stepped into this line it says hbm = NULL, and the GetLastError function returns 0 which I've read means function was successful. I've tried passing NULL instead of hInstance, I've tried passing the filename in as LPCSTR szFileName -- I declared it as LPCSTR szFileName("C:\Users\Jemma\Desktop\Maze Game\Assets\TILE_01.bmp"). Absolutely no idea what to try next. Any help on this would be greatly appreciated.

2
0xcccccccc is the standard value in debug builds for an uninitialized stack variable. Your code looks correct. I even tested it with my own image. When LoadImage returns NULL You need to call GetLastError to get the error code for why it failed. Are you sure that TILE_01.bmp exists and is a real BMP file and not some other format? - selbie
Thanks for your fast response, I had a PNG file that I converted to BITMAP. I've just downloaded a BMP file and it seems to work fine now :) Thanks again - Jemma
MFC is very limited on what image files it can accept. If your image has transparencies, you will need to save the file in a very specific format as in my own comment: stackoverflow.com/questions/41953499/… - sergiol

2 Answers

1
votes

First of all, make sure that your image is real bmp file and can be opened with image viewer. Then try this to load that HBITMAP using LoadImage:

const char* filename = "C:\\Users\\Jemma\\Desktop\\Maze Game\\Assets\\TILE_01.bmp";
HBITMAP bmp = (HBITMAP)LoadImage(NULL, filename,
    IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hbm == NULL)
{
    DWORD lastError = GetLastError();
    return 1;
}

Note, you need to use 0, 0 for sizes when you use LR_DEFAULTSIZE, also, when loading from file you need to use LR_LOADFROMFILE. All of these are mentioned in documentation of LoadImage function.

When stepping through with debugger if you get something unexpected you need to check GetLastError. If you are stepping through with VS debugger, you can simply add @err in watch window and you'll always see the last error without changing your code. You can also add it as @err,hr and it will show you readable description of the error that happened.

0
votes
const char* filename = "C:\\Users\\Jemma\\Desktop\\Maze Game\\Assets\\TILE_01.bmp";
HBITMAP bmp = (HBITMAP)LoadImage(NULL, filename,
    IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (bmp== NULL)
{
    DWORD lastError = GetLastError();
    return 1;
}

I am using the same code as above mentioned but LoadImage() is returning NULL and getting lasterror =2 , even i am sure file is already there from belowcode:

string filePath = "D:\\ACC_car_Ego.bmp";
    if (boost::filesystem::exists(filePath))    // does filePath actually exist?
        DWORD lastError = GetLastError();
    else
        DWORD lastError1 = GetLastError();