Win32 application. HBITMAP LoadImage fails to load anything

﹥>﹥吖頭↗ 提交于 2019-12-01 06:27:33

Your early versions of the question checked the return value of LoadImage but then did nothing more. The documentation says this:

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

So, if the function fails, call GetLastError to find out why. When you do so, you obtain the error code ERROR_FILE_NOT_FOUND. Which is pretty conclusive. The filename that you specified does not exist.

Do note that the code in the latest update to the question calls GetLastError unconditionally. That is a mistake, one that is seen all too frequently here on Stack Overflow. The documentation only tells you to call GetLastError when the call to LoadImage fails. If the call to LoadImage succeeds, then the value return by GetLastError is meaningless. Error handling in Win32 is largely handled in the same way as LoadImage does it, but not always. So you have to read the documentation very carefully.


Perhaps instead of

C:\\Users\\Tim\documents\\...

you meant

C:\\Users\\Tim\\documents\\...

OK, now you've got the path right. The call to LoadImage returns NULL, but GetLastError is no longer helpful and returns ERROR_SUCCESS. Which is weird in itself.

I believe that the problem is that your image uses a format that LoadImage does not understand. I took your .bmp file, loaded it in Paint.net and then re-saved it. Once I did that, the re-saved image was loaded successfully.


Rather than trying to load this from a file it would make far more sense to link the image as a resource and load it that way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!