Cannot load a bitmap using the winapi c++ - c++

I am pretty new to c++ and the windows api and am currently trying to create a calculator application. I would like to place images of certain mathematical symbols on buttons, but I am having trouble getting the images to load. Whenever I use LoadImage() or LoadBitmap() to try and load the image, the function returns null.
In a resource file, I have written:
SQROOT BITMAP "sqroot.bmp"
In a file called Resource.h, I have written:
#define SQROOT 201
In order to load the bmp, I have tried:
HBITMAP bmp = (HBITMAP)LoadImage((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCE(SQROOT), IMAGE_BITMAP, 20, 20, LR_DEFAULTCOLOR);
if (bmp == NULL) {
MessageBox(hWnd, L"Image failed to load", L"Error", 1);
}
I have also tried:
HBITMAP bmp = LoadBitmap((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCE(SQROOT));
if (bmp == NULL) {
MessageBox(hWnd, L"Image failed to load", L"Error", 1);
}
In the resource file, I have also tried putting the complete path to the bmp but that also did not work.
Both of these functions return null, would anyone happen to know what I am doing wrong? Thanks.

When opening the executable using visual studio, I discovered that my bitmap was in an invalid format. To solve this issue, I imported the bitmap into Microsoft Paint and then exported it back into my project and that solved the issue.

Related

Get png file data from a png stored as a Windows resource (c++) [duplicate]

This question already has answers here:
Load resource as byte array programmaticaly in C++
(2 answers)
How to load a custom binary resource in a VC++ static library as part of a dll?
(3 answers)
Closed 2 years ago.
I have a PNG resource stored in a Windows exe. I want to load the png resource and create a pointer to the png data that can be written to disk to create a png file. (FWIW, I don't actually create the file at this point; the data is initially written inside another file and saved as a png file later.) This is in an MFC (c++) project, but the solution doesn't need to use MFC.
I envision a function prototype like this:
void GetPngFromResource(int iResourceID, VOID* pPngFileData, DWORD* pwPngDataBytes);
I see that ::LoadBitmap(), ::LoadImage() and CImage::LoadFromResource() don't support png resource loading. When I do attempt to use LoadImage(), GetLastError() reports "The specified resource name cannot be found in the image file" (error 1814):
HBITMAP hBitmapImage = (HBITMAP)::LoadImage(::GetModuleHandle(0), MAKEINTRESOURCE(iResourceID), IMAGE_BITMAP, 0, 0, 0);
I know the resource exists, because I can get a handle to it using FindResource() (although I don't know how, or whether it is possible, to get the png file data from the HRSCR handle):
HRSCR h = FindResource(HINST_THISCOMPONENT, MAKEINTRESOURCE(iResourceID), L"PNG");
I have seen examples that create an HBITMAP from a png resource using gdi+, but I need to end up with png file data.
Thanks!
"john" deserves credit for this correct solution, as he led me to it (via comments) [yeah, the SO gods marked my question as a duplicate and I can't select this as the solution, but it is].
void GetPngFromResource(int iResourceID, void** ppPngFileData, DWORD* pwPngDataBytes)
{
HMODULE hMod = GetModuleHandle(NULL);
HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(iResourceID), _T("PNG"));
HGLOBAL hGlobal = LoadResource(hMod, hRes);
*ppPngFileData = LockResource(hGlobal);
*pwPngDataBytes = SizeofResource(hMod, hRes);
}
I've excluded error trapping for brevity.

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.

How to load a bitmap from a resource file (.rc file)?

I am programming a simple game in visual studio and I have set up a resource file (.rc file) i am also using sdl2. I am wondering if there is a way to load or draw the bit maps located in the resource file. Thanks in advance
I am currently using this line:
HBITMAP hBtMpIMG = LoadBitmap((HINSTANCE)getModuleHandle(_T("Project 1.exe")), MAKEINTRESOURCE(IDB_BITMAP1));
How would I render the hBtMpIMG using sdl2?
You can use the API: LoadBitmap to load a bitmap stored in the executable:
case WM_CREATE:
{
HBITMAP hBtMpBall = LoadBitmap((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDB_BALL)); //Here we have to use the executable module to load our bitmap resource
//this means that this resource "ball.bmp" is compiled and stored in the executable module"
//however if you use loadimage you can ignore this module and makeit null because you are laoding from file
if(!hBtMpBall)
MessageBox(0,"ball.bmp not found!",0,0);
}
break;
In a resource file: .rc you may have like this:
#include "myres.h"
IDB_BALL BITMAP DISCARDABLE "ball.bmp"

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

Loading an image from a resource embedded in a dll

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.