-1

I am new to win32. I placed the bitmap image 'logo2.bmp' into the directory which .vcxproj is there. However, when I used the LoadImage() function, it seems that nothing is loaded.

I have checked my questions online, but the image still cannot be loaded after the modification.

void AddControls(HWND hwnd)
{
//some code
    hLogo = CreateWindowW(L"Static", NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP,
                                 350, 60, 100, 100, hwnd, NULL, NULL, NULL);
    if (hLogoImage != NULL)
    {
        SendMessage(hLogo, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hLogoImage);
    }
    else
    {
        MessageBox(0, L"Could not load image", L"Error", MB_OK);
    }
}

void loadImages()
{
    hLogoImage = (HBITMAP)LoadImage(NULL, L"logo2.bmp", IMAGE_BITMAP,
                                      0,0,LR_LOADFROMFILE | LR_DEFAULTSIZE);
}

I expect the output is a bitmap image successfully displayed on the windows, but the 'Error' text box was displayed and no picture displayed instead.

  • 1
    When `LoadImage` has returned NULL, what does `GetLastError` return. If it is "file not found", what is the current directory? does logo2.bmp exist in that directory? – Martin Bonner supports Monica Jun 25 '19 at 11:32
  • Just read your second sentence. Are you running this code from Visual Studio? If so, I *think* the current directory is the directory containing the .exe. – Martin Bonner supports Monica Jun 25 '19 at 11:34
  • 3
    The majority on winapi questions that are asked here contain code that does not include error checking. Irrespective of what is actually wrong with your code, the biggest lesson for you is that you must learn to check for errors when you use the Windows API. – David Heffernan Jun 25 '19 at 11:38
  • I see. Let me check the directory – Poon Sloth Jun 25 '19 at 13:25
  • 1
    There is no conceivable reason to **ever** use relative path names in code. **Always** construct a fully qualified path name, if you do not want your code to change its meaning given the environment you do not control. – IInspectable Jun 25 '19 at 13:54
  • Possible duplicate of [Use relative file path with LoadImage](https://stackoverflow.com/questions/15719548/use-relative-file-path-with-loadimage) – Raymond Chen Jun 25 '19 at 14:00
  • em... cannot load bitmap image after using an absolute path – Poon Sloth Jun 25 '19 at 15:17
  • I only got this warning so far. Warning C4715 'WindowProcedure': not all control paths return a value – Poon Sloth Jun 25 '19 at 15:38
  • 1
    I wasn't suggesting that you use an *absolute* path. I was suggesting that you construct a *fully qualified* path. That path can be constructed relative to a known location (like the executable image's path, available through [GetModuleFileName](https://learn.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-getmodulefilenamew)). The warning you get is pretty clear: There's at least one path through your window procedure, that doesn't return a value. Fix that. – IInspectable Jun 25 '19 at 16:53
  • I used GetLastError and the value is 0 !! – Poon Sloth Jun 26 '19 at 09:32
  • Guys, my question is very similar to this: https://stackoverflow.com/questions/20584045/win32-application-hbitmap-loadimage-fails-to-load-anything – Poon Sloth Jun 26 '19 at 09:39
  • I also tried to do this when the message WM_PAINT is received, but the same warning is received. – Poon Sloth Jun 26 '19 at 09:55

1 Answers1

-1

Edit: Okay, I understand that this is a problem of file format. I better study more.

  • What's the size of your .bmp file in both cases when it works and when it doesn't? – Jabberwocky Jun 26 '19 at 12:27
  • The original picture: bmp format, size: 108 KB The new image: bmp format, 1 KB – Poon Sloth Jun 26 '19 at 13:35
  • Anyway, Thank you so much to all of you for reminding me so many things, including getlasterror() – Poon Sloth Jun 26 '19 at 13:36
  • `LoadImage` has **no** problem loading a bitmap image that's just 100k in size. It's way more likely that your initial bitmap was violating the BMP file format. Lots of applications write invalid BMP files. To verify this, load the image into MS Paint, and save it from there. That will always produce a valid BMP file. Anyway, this proposed answer fails to analyze the issue. It's not useful, and the casing makes it seriously annoying to look at. – IInspectable Jun 27 '19 at 12:27
  • I see, many thanks for the comment! I will delete the answer and thank you for reminding me! – Poon Sloth Jun 29 '19 at 02:34