Skip to main content
GameDev.net gamedev.net
🔒 Locked

Save bitmaps in HDC's

Started by danne89 Jan 20, 2006 at 5:10 PM 5 replies 1.5k views
Original Post
danne89
danne89
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/
Colin Jeanne
Colin Jeanne
You must first select a bitmap of the appropriate dimensions and bitdepth into hLogo. The default bitmap in memory DCs is 1x1 and monochrome.
Colin Jeanne
Colin Jeanne
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
danne89
danne89
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/
Dim_Yimma_H
Dim_Yimma_H
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]
danne89
danne89
Ohh! How awfully ambarresed i am.
http://www.cis.gsu.edu/~shong/oojokes/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.