1

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.

Jemma
  • 13
  • 1
  • 3
  • 1
    `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](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx) 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 Apr 19 '17 at 22:50
  • 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 Apr 19 '17 at 23:05
  • 1
    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: http://stackoverflow.com/questions/41953499/mfc-toolbar-icon-not-visible-in-visual-studio-2015#comment71572831_41953499 – sergiol Apr 20 '17 at 17:37

2 Answers2

1

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.

Pavel P
  • 15,789
  • 11
  • 79
  • 128
  • Thanks for the tip. I followed your advice and after another few hours of debugging :/ I've found that after the loadImage() function @err,hr says: "ERROR_INVALID_HANDLE : The handle is invalid" – Jemma Apr 20 '17 at 15:59
  • @Jemma try to use `LoadImage((HINSTANCE)NULL, ...` – Pavel P Apr 20 '17 at 16:09
  • Hi, just tried that and it still says invalid handle, I've also tried `LoadImage((HINSTANCE) hInstance,... ` just incase and it still comes up with invalid handle, I've noted that under autos while debugging the hInstance value says Maze Game.exe!0x00810000(Type information missing from symbol file) {unused=946030} not sure if that could be causing issues – Jemma Apr 21 '17 at 10:05
  • @Jemma Are you still having this issue? if not how did you fix it? – محمد خير الخلق Aug 13 '20 at 13:50
  • I got it working by saving the bitmap with 24 bits only. With 32 bits it didn't work. – Biesi Nov 24 '20 at 20:00
0
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();