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"
Related
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.
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
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.
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".
I've been working on testing a few things out using SFML 1.4 (Simple and Fast Multimedia Library) with C++ and Visual C++ 2008 Express Edition. To avoid having external images with my graphical programs, I was testing out the sf::Image::LoadFromMemory(const char * Data, std::size_t SizeInBytes) function with Bitmap resources loaded using a simple resource script:
IDB_SPRITE BITMAP "sprite1.bmp"
In my code to load the image to create an sf::Image using this bitmap resource, I use the following procedure, consisting of Win32 API functions (I've excluded the code that checks to make sure the Win32 functions don't return NULL to shorten this a bit):
HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDB_SPRITE), RT_BITMAP);
HGLOBAL hResData = LoadResource(NULL, hResInfo);
char * resourceData = reinterpret_cast<char *>(LockResource(hResData));
After that, I use the sf::Image::LoadFromMemory function:
MyImage.LoadFromMemory(resourceData, SizeofResource(NULL, hResInfo));
However, this doesn't work (I get an unknown file type error). After some testing, I discovered that the bitmap data I pass to the LoadFromMemory function does not include the BITMAPFILEHEADER (the first 14 bytes), and I believe this is the cause of the unknown file type error.
I can restore the BITMAPFILEHEADER manually and get the LoadFromMemory function to work fine. However, I'm wondering if there is some way to preserve the BITMAPFILEHEADER in the resource data to avoid doing this?
Using a custom resource type will preserve the entire file. Change the resource script to utilize the RCDATA type as opposed to the BITMAP type:
IDB_SPRITE RCDATA "sprite1.bmp"
In the FindResource function call, use RT_RCDATA instead of RT_BITMAP:
HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDB_SPRITE), RT_RCDATA);
For more information:
RCDATA Resource
Resource Types
You can add file to resources as a custom resource instead of RT_BITMAP -- this will add file exactly as it is. Unless you also need to ::LoadImage() it.