Loading Bitmap from resource (HELP)

So I've got my bitmap in my .rc file, but i don't know what direction to go from there.
loading, blit-ing, and displaying a bitmap from a file is easy enough. what's the best way to do the same thing but with a resource.

My function for loading bitmaps from a file goes thusly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool makebitmap(LPCWSTR szFileName, HDC hWinDC, int bmposx, int bmposy, int sizex, int sizey)
{
	HBITMAP hBitmap;

	hBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
		LR_LOADFROMFILE);

	if(hBitmap == NULL)
	{
		MessageBox(NULL, _T("Loadimage failed"), _T("Error"), MB_OK);
		return FALSE;
	}

	HDC hLocalDC;
	hLocalDC = CreateCompatibleDC(hWinDC);

	if(hLocalDC == NULL)
	{
		MessageBox(NULL, _T("Device context creation failed"), _T("Error"), MB_OK);
		return FALSE;
	}

	BITMAP qBitmap;
	int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP),
		reinterpret_cast<LPVOID>(&qBitmap));
	if (!iReturn)
	{
		MessageBox(NULL, _T("GetObject failed"), _T("Error"), MB_OK);
		return FALSE;
	}

	HBITMAP hOldBmp = (HBITMAP)SelectObject(hLocalDC, hBitmap);
	if(hOldBmp == NULL)
	{
		MessageBox(NULL, _T("Holdbmp failed"), _T("Error"), MB_OK);
		return FALSE;
	}

	BOOL qRetBlit = BitBlt(hWinDC, bmposx, bmposy, qBitmap.bmWidth, qBitmap.bmHeight,
		hLocalDC, 0, 0, SRCCOPY);
	if (!qRetBlit)
	{
		MessageBox(NULL, _T("Blit failed"), _T("Error"), MB_OK);
		return FALSE;
	}

	SelectObject (hLocalDC, hOldBmp);
	DeleteDC(hLocalDC);
	DeleteObject(hBitmap);

	return TRUE;
}


and my call to the function in the WM_PAINT handler
1
2
3
4
5
6
case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
                makebitmap(_T("szfilename.bmp"), hdc, 1000, 500, 32, 32);
		EndPaint(hWnd, &ps);
		break;


how can i change this so that it loads the bitmap from the resource.
Last edited on
(HBITMAP)::LoadImage(NULL, MAKEINTRESOURCE(IDC_YOURBITMAPID), IMAGE_BITMAP, 0, 0, 0);

EDIT:

Also, you REALLY shouldn't be loading/destroying the bitmap on every WM_PAINT message (possibly several times per second!)

You should load the image once (possibly when the window is created), then destroy it when it's no longer needed. Loading is very expensive.
Last edited on
I tried MAKEINTRESOURCE but it just won't load the image.
my handle to the bitmap ends up being null somehow.

And how do you recommend displaying a bitmap outside WM_PAINT: since the variable that gets passed as the device context on the call to makebitmap is initialized as BeginPaint(hWnd, &ps);
Last edited on
Did you forget to replace LR_LOADFROMFILE with zero?

And how do you recommend displaying a bitmap outside WM_PAINT: since the variable that gets passed as the device context on the call to makebitmap is initialized as BeginPaint(hWnd, &ps);


I don't recommend displaying it outside WM_PAINT. I recommend loading it outside WM_PAINT.

You can make the HBITMAP and offscreen HDC anywhere. And you should make them exactly once. When WM_PAINT happens, you just do a quick BitBlt to display it.
Last edited on
Ha! i got it now, first parameter of LoadImage had to be GetModuleHandle(I guess this is equivalent to hwnd?)

thanks for clearing up the WM_PAINT thing for me, I gotcha now;
HMODULE is the same as HINSTANCE. The value is the virtual address where the .exe or .dll is mapped into your process' address space. It has nothing to do with HWND.
Anyways you should load your bitmap when having a WM_INITDIALOG or WM_CREATE, and release it when having a WM_CLOSE or any other user-input action (Like File->Quit).
Topic archived. No new replies allowed.