MrAszter
MrAszter

Reputation: 15

Cannot get an image handle from a bitmap within the resources when using "LoadImageA()" and cannot understand why

I am trying to load an image resource using the LoadImageA() function, yet it doesn't work and I don't understand why.

Here's a bit of my code :

bool isRessource = IS_INTRESOURCE(107);

// Load the resource to the HGLOBAL.
HGLOBAL imageResDataHandle = LoadImageA(
    NULL,
    MAKEINTRESOURCEA(107),
    IMAGE_BITMAP,
    0,
    0,
    LR_SHARED
    );
HRESULT hr = (imageResDataHandle ? S_OK : E_FAIL);

The image I want to load is a bitmap saved in the resources, and represented as such within resources.h:

#define IDB_BITMAP1                     107

When I execute the code, isRessource is equal to true, yet hr is equal to E_FAIL.

Any idea as to why this is happening? I am using Visual Studio 2019, and I made the image using Gimp.

Upvotes: 0

Views: 471

Answers (2)

Strive Sun
Strive Sun

Reputation: 6289

The first link searched with LoadImage gimp as a keyword is enough to answer this question.

This is some useful information:

The bitmap exported by GIMP has a broken header. Specifically, the code seems to not write the RGBA masks, which AFAIK are not optional in a BITMAPV5HEADER. This misaligns and changes the size of the entire extended header, incidentally making it look like a BITMAPV4HEADER, which explains why most programs will still open it fine. Without having done any testing, I'd guess LoadImage() is more picky about the values in this extended header; returning NULL is how it indicates failure.

By the way, when you import a bitmap, the system does not remind you that the format of the image is unknown?

Like:

enter image description here

After testing, use LoadImage to load such an image will return NULL, and GetLastError will also return 0.

Upvotes: 0

MrAszter
MrAszter

Reputation: 15

After making the same image with the same format on another application (I used "Krita") and importing it again, the image finally loads with the same code (I only changed the reference to the resource). I guess that all types of bitmaps made from Gimp won't work in Visual Studio (I tried most formats of bitmaps from Gimp).

Upvotes: 1

Related Questions