Save bitmaps in HDC's

Started by
5 comments, last by danne89 18 years, 2 months ago
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);

http://www.cis.gsu.edu/~shong/oojokes/
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.
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?
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 doneHBITMAP hOldBMP = (HBITMAP)SelectObject(hLogo, hBitmap);


Then you can blit
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;
http://www.cis.gsu.edu/~shong/oojokes/
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 variablesstatic HDC hLogo;static HBITMAP hBitmap, hOldBMP;//temporary variablesHDC 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]
Ohh! How awfully ambarresed i am.
http://www.cis.gsu.edu/~shong/oojokes/

This topic is closed to new replies.

Advertisement