• Advertisement
Sign in to follow this  

Save bitmaps in HDC's

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Can anyone try to explain why you cannot do like this:
		case WM_CREATE:
			hdc = GetDC(hWnd);
			hLogo = CreateCompatibleDC(hdc);
			ReleaseDC(hWnd, hdc);	

			for (y = 0; y < logo_height; y++)
			{
				for (x = 0; x < logo_width; x++)
				{
					HEADER_PIXEL(logo_data, pixel);
					SetPixel(hLogo, x, y, RGB(pixel[0], pixel[1], pixel[2]));
				}
			}
	
	
			return 0;

		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			BitBlt(hdc, 0, 0, logo_width, logo_height, hLogo, 0, 0, SRCCOPY);
			EndPaint(hWnd, &ps);

Share this post


Link to post
Share on other sites
Advertisement
You must first select a bitmap of the appropriate dimensions and bitdepth into hLogo. The default bitmap in memory DCs is 1x1 and monochrome.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Like this?

hdc = GetDC(hWnd);
hLogo = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);

hBitmap = CreateCompatibleBitmap(hLogo, logo_width, logo_height);
SelectObject(hLogo, hBitmap);
for (y = 0; y < logo_height; y++)
{
for (x = 0; x < logo_width; x++)
{
HEADER_PIXEL(logo_data, pixel);
SetPixel(hLogo, x, y, RGB(pixel[0], pixel[1], pixel[2]));
}
}


And then just blit?

Share this post


Link to post
Share on other sites
If you create the bitmap to be compatible with hLogo then it will still be a monochrome bitmap. You'll need to create it so that it is compatible with your original DC. Do something like this instead:


hdc = GetDC(hWnd);
hLogo = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, logo_width, logo_height);
ReleaseDC(hWnd, hdc);

// Remember to store the old bitmap in hLogo so that you can properly
// clean everything when you're done
HBITMAP hOldBMP = (HBITMAP)SelectObject(hLogo, hBitmap);




Then you can blit

Share this post


Link to post
Share on other sites
It still doesn't work:


case WM_CREATE:
hdc = GetDC(hWnd);
hLogo = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, logo_width, logo_height);
ReleaseDC(hWnd, hdc);

hOldBMP = (HBITMAP)SelectObject(hLogo, hBitmap);

for (y = 0; y < logo_height; y++)
{
for (x = 0; x < logo_width; x++)
{
HEADER_PIXEL(logo_data, pixel);
SetPixel(hLogo, x, y, RGB(pixel[0], pixel[1], pixel[2]));
}
}
return 0;


case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
BitBlt(hdc, 0, 0, logo_width, logo_height, hLogo, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
return 0;

Share this post


Link to post
Share on other sites
Are hLogo and hBitmap local temporary variables? Make sure they aren't because then they will only refer to a valid bitmap during the WM_CREATE message.

Example:

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//static variables
static HDC hLogo;
static HBITMAP hBitmap, hOldBMP;

//temporary variables
HDC hdc;
PAINTSTRUCT ps;
int x, y;

switch(msg)
{
case WM_CREATE:
hdc = GetDC(hWnd);
hLogo = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, 100, 100);
ReleaseDC(hWnd, hdc);

hOldBMP = (HBITMAP)SelectObject(hLogo, hBitmap);

for (y = 0; y < 100; y++)
{
for (x = 0; x < 100; x++)
SetPixel(hLogo, x, y, RGB(255, 255, 0));
}
return 0;


case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
BitBlt(hdc, 0, 0, 100, 100, hLogo, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
return 0;

case WM_DESTROY:
//free the created bitmap and device context
DeleteDC(hLogo);
DeleteObject(hBitmap);

PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}




I think that might have been the only problem in your last code actually [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement