Loading an image from a resource embedded in a dll - c++

I'm developing a browser plugin (npapi) using FireBreath, and in it I'm trying to embed images which I need to load at runtime.
I've been search, reading and experimenting for two days now but I can't seem to make it work.
This is the code I ended up with:
HMODULE hModule;
LPCWSTR path = L"nptest.dll";
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, path, &hModule);
LPTSTR resourceName = MAKEINTRESOURCE(106);
HBITMAP bitmap = (HBITMAP) LoadImage(hModule, resourceName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
The loaded bitmap seems to be empty after the execution of this code, and I can't figure out why.
At first I tried to load a PNG image, and later realized that PNGs are not supported, so I just created a BMP file using the resource editor in VS2010.
I know for sure that the BMP (with id 106) is in the compiled DLL because I used PE Explorer to check inside the DLL.
Any ideas as for why it fails to load the image resource?
Thanks a lot.

Related

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.
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).
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:
After testing, use LoadImage to load such an image will return NULL, and GetLastError will also return 0.

LoadImage Function isn't Working With ImageMagick Created Bitmap Images

I am trying to load some bitmap images with my application to do some comparisons whose created using ImageMagick using following command:
C:\Program Files\ImageMagick-7.0.6-Q16>Convert << SOURCE BMP >> -crop -650 << DEST BMP >>
Above source bitmap image loads fine with LoadImage API Function, but the image created by ImageMagick (destination bitmap) fails to load.
How I am using LoadImage Function:
HBitmapRight = (HBITMAP)LoadImage(nullptr, L"C:\\DEST.bmp", IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE);
HBitmapRight = (HBITMAP)LoadImage(nullptr, L"C:\\SOURCE.bmp", IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE);
I used GetLastError, but it returned ERROR_SUCCESS.
Upon my research, I found this answer and so, this maybe a encoding issue or something similar.
Unfortunately I cannot use any tools like MS Paint or Photoshop, but I only can use ImageMagick because this comparison is done using command line without user interaction.
Is it possible to set any special encoding flags to be used with ImageMagick, so bitmap images creating by it can be compatible with LoadImage?
I am running this application in my Windows 7 64-bit machine and installed ImageMagick is also the latest 64-bit version of it. Compiler is MS Visual C++
2017.
Those are the lines that causes the issue:
<Mask>
<Red/>
<Green/>
<Blue/>
<Alpha/>
</Mask>
<ColorSpaceType/>
<CIEXYZEndPoints>
<Red>
<X/>
<Y/>
<Z/>
</Red>
<Green>
<X/>
<Y/>
<Z/>
</Green>
<Blue>
<X/>
<Y/>
<Z/>
</Blue>
</CIEXYZEndPoints>
<Intent/>
These lines are in XML metadata of ImageMagick created image. I checked with source image, it doesn't have those lines.
Please help me to resolve this issue without user interaction.
I fixed the problem by changing my ImageMagick command to use specific version of Bitmap format like this:
C:\Program Files\ImageMagick-7.0.6-Q16>Convert << SOURCE BMP >> -define bmp:format=bmp3 -crop -650 << DEST BMP >>
Now LoadImage API function works fine with ImageMagick created bitmap images.

Trying to load a Bitmap from Resources returns NULL with an error code of 1813

I've been trying to load a bitmap from my resources in order to set it as an icon in one of my programs control for a good while now, with no success at all.
What I did so far:
First, I went into the code of my .rc file, and added all my bitmaps like this in the corresponding BMP section. Note that all .bmp files are saved as 256-color-bitmaps:
IDB_01d BMP "<path>"
Afterwards, I went into my Resource.h file and inserted a define for every bitmap, looking like this:
#define IDB_01d 2000
After adding my resources, this is what I tried in my code so far:
HBITMAP hbmp = LoadBitmap(m_hInstance, MAKEINTRESOURCE(IDB_01d));
DWORD lastError = GetLastError();
m_weatherIcon.SetIcon(hbmp);
Note that m_hInstance is the HINSTANCE i got from my _tWinMain-method.
However, this is not working. The problem is currently the fact, that hbmp is NULL, due to LoadBitmap returning a NULL value.
I added a call to get the last error, and the error code I'm getting is 1813 all the time. I already did some research, and it seems like the HINSTANCE might be the problem, but I don't see how exactly.
Any advice?
you need declare in .rc file
IDB_01d BITMAP "<path>"
but you using unknown resource type BMP

SDL and Visual Studio 2010 resources

I have a simple question. I use SDL and SDL_image in my c++ program and image loading is fine from a single png file.
SDL_Surface *dot = NULL;
dot = load_image("dot.png");
But how can I load the png file if I add it to the resources? so I don't want to store in a png file next to the exe. Is it possible to load from the resources?
Tried
dot = load_image(MAKEINTRESOURCE(IDB_PNG1));
but it didn't work.
It is fully possible to load an image or something else into SDL from a windows resource. To do this you need to get the raw data and pass it to the appropriate RWOPS.
HMODULE hModule = GetModuleHandle(_T("myapp.exe"));
HRSRC hWhite = FindResource(hModule, MAKEINTRESOURCE(IDB_WHITE_PNG), _T("PNG"));
unsigned int white_size = SizeofResource(hModule, hWhite);
HGLOBAL hgWhite = LoadResource(hModule, hWhite);
unsigned char* white_data = (unsigned char*)LockResource(hgWhite);
SDL_Surface* white = IMG_Load_RW(SDL_RWFromConstMem(white_data, white_size), 1);
This assumes that you have something similar in your *.rc file:
IDB_WHITE_PNG PNG "White.png"
According to the MAKEINTRESOURCE documentation :
The return value should be passed only to functions which explicitly indicate that they accept MAKEINTRESOURCE as a parameter.
You don't give the content of load_image (BTW, please include the content of the functions you use in your question, you'll get better answers ...) but I bet it's not using its parameter to call one of the Windows SDK functions which accept MAKEINTRESOURCE ... as far as I know these resources are supposed to hold some specific Windows UI data like mouse cursors, icons, etc.. for use with Windows functions, not with other libraries like SDL, so I'm not surprised it doesn't work.

Loading JPEG file from resources in MFC C++ application

The following code works correctly under Windows XP:
CImage image;
RECT destRect;
int nResource = 10;
CResourceStream stream(0, MAKEINTRESOURCE(nResource), _T("JPEG"));
HRESULT hr = image.Load(&stream);
image.Draw(hDC, destRect);
But on Windows 7 image.Load returns E_FAIL though creating CResourceStream reads JPEG file from resources correctly.
Debugging gives the following result:
GdipCreateBitmapFromStream returns InvalidParameter.
What the problem can be?
JPEG is a custom category in resource file.
At the end I used this solution from codeproject:
http://www.codeproject.com/KB/GDI-plus/cgdiplusbitmap.aspx
It is a thin wrapper for GDI+ which is able to load JPEG files (and others) under Windows 7 perfectly.
I believe you should use "JPG" not "JPEG".