• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

CreateCompatibleBitmap

suityou01

Potty-trained
Thanks received
222
I have used one of these to do a screen grab. I have a device context to my GTK image widget, and want to load my bitmap into the image

code is below.

So I think I have done the screen grab ok, I just cannot see the end result. I would like to load it into the Image box I have, but dumping to file is just as good. Been googling this for a while now, and everything I try does not work.

What is the shortest way to get this working? Any suggestions? This is only for POC so no need to go to town on it.

Code:
int doScreenGrab(HANDLE clientDC)
{
	HANDLE hScreenDC;
	HANDLE hMemDC;
	HANDLE hWnd;
	HGDIOBJ     hOldBitmap , hBitmap;
	
	long xWidth = GetSystemMetrics(SM_CXSCREEN);
	long yHeight = GetSystemMetrics(SM_CYSCREEN);
	
	g_print("Desktop metrics xWidth : %d yHeight : %d \n",xWidth,yHeight);
	
	hScreenDC = GetDC(0);
	hMemDC = CreateCompatibleDC(hScreenDC);
	
	g_print ("Desktop Device Context %d \n", hScreenDC);
	g_print ("Memory Device Context %d \n", hMemDC);
	hWnd = WindowFromDC(hScreenDC);
	g_print ("Desktop hWnd %d \n", hWnd);
	
	hBitmap = CreateCompatibleBitmap(hScreenDC, xWidth, yHeight);   
	hOldBitmap =   SelectObject (hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 100, 100, hScreenDC, xWidth, yHeight, SRCCOPY);

	hBitmap = SelectObject(hMemDC, hOldBitmap);   
	BitBlt(clientDC, 0, 0, 100, 100, hMemDC, xWidth, yHeight, SRCCOPY);
	
	DeleteDC(hScreenDC);     
	DeleteDC(hMemDC);     
}
 
You've got the 7th & 8th parameters of both BitBlts wrong. It's xsrc, ysrc, not width and height, so should be 0,0.

BitBlt won't stretch. If you're expecting to get a small thumbnail of the whole screen, you want StretchBlt.

Code:
// copy whole screen to Memory bitmap
BitBlt(hMemDC, 0, 0, xWidth, yHeight, hScreenDC, 0, 0, SRCCOPY);

hBitmap = SelectObject(hMemDC, hOldBitmap);

// stretch to 100,100
StretchBlt(clientDC, 0, 0, 100, 100, hMemDC, 0, 0, xWidth, yHeight, SRCCOPY);
 
You've got the 7th & 8th parameters of both BitBlts wrong. It's xsrc, ysrc, not width and height, so should be 0,0.

BitBlt won't stretch. If you're expecting to get a small thumbnail of the whole screen, you want StretchBlt.

Code:
// copy whole screen to Memory bitmap
BitBlt(hMemDC, 0, 0, xWidth, yHeight, hScreenDC, 0, 0, SRCCOPY);

hBitmap = SelectObject(hMemDC, hOldBitmap);

// stretch to 100,100
StretchBlt(clientDC, 0, 0, 100, 100, hMemDC, 0, 0, xWidth, yHeight, SRCCOPY);


Thanks VM. That helped. :banana:
 

Advertisers

Back
Top