Stencil
Stencil

Reputation: 1873

Windows GDI Context- LoadImage and GetLastError()

A call to LoadImage() in the first steps of my program returns NULL. Just after it, GetLastError() is called, and it surprisingly returns 0. I wondered why LoadImage() is failing, since GetLastError() clearly reveals that no error code is set after the failed function. This is a snippet of the code:

if ( (hbitmap = (HBITMAP) LoadImage(hThisInstance, MAKEINTRESOURCE(MY_BITMAP), 
                                    IMAGE_BITMAP, 0, 0, 
                                    LR_CREATEDIBSECTION)) == NULL) 
    printf("Last error: %d\n", GetLastError());

With HBITMAP hbitmap, HINSTANCE hThisInstance (argument of WinMain), and MY_BITMAP a valid bitmap resource.

Upvotes: 1

Views: 1761

Answers (2)

Duncan Mackay
Duncan Mackay

Reputation: 31

I found exactly the same behaviour with the latest version of GIMP. When you're doing an export from Gimp, select the Compatibility Options [+] button and check the "Do not write colourspace information" checkbox, and the bitmap will load OK using LoadImage. Edit: I see a prior comment to that effect now, otherwise I might not have bothered writing!

Upvotes: 3

digory doo
digory doo

Reputation: 2310

Apparently, LoadImage returns NULL and does not set any error when there's a problem with the file format.

A copy of the bitmap I was trying to load can be found here. I created it using GIMP, which in the current version is quite buggy.

The solution was to open the file in Paint and save it again. Now LoadImage loads the image without any complaints.

Upvotes: 5

Related Questions