How to take Screen Shot C++

Hi,

I am trying to take screen shot, can someone tell me using code please becuase all ones I tried don't even work. Thanks in advanced
Show us what you've gotten so far and maybe we can help you understand what is going wrong.
That's not really very helpful, since there isn't a whole lot of variability in how to do it.

Google "msdn capturing an image" and click "I'm feeling lucky" for the example C++ code to do the kind of thing you want. It is pure Win32, but you can easily adapt it to any other framework -- the important stuff is all in the CaptureAnImage() function.

Make sure to spend a little time googling "msdn function_name" for the functions used in there to understand what is going on and how to adjust the parameters of the capture.

Good luck!
Code:
WHATS WRONG
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,char* szPath);

void CaptureActiveWindow(void)
{
	RECT ActWndRect;

	WCHAR buf [100],buf1[20];
	int xSrc=0,ySrc=-19;
	int DepcWidth=10, DepcHeight=5;
	//OutputDebugString(L"Start capture act window ");	
	HWND s = GetDesktopWindow();
	HDC ActWndDC = GetDC(s);				//DC for the window you have clicked on	

	HDC MemDC = CreateCompatibleDC(ActWndDC);			//Memory DC Compatible with Above DC

	GetWindowRect(s,&ActWndRect);			//Will Store the Windows Are in Rectangle 

	//wsprintf(buf,L"x1 = %d , y1 = %d, x2 = %d y2 =%d",ActWndRect.left,ActWndRect.top,ActWndRect.right,ActWndRect.bottom);
	//OutputDebugString(buf);	

	int Width = ActWndRect.right-ActWndRect.left + 50;		//Width of the Window
	int Height =ActWndRect.bottom-ActWndRect.top + 50;		//Hight of the Window

	//if(GetWindowText(s,buf1,20) >0)
	//{
	//	OutputDebugString(buf1);
	//}
	
	HDC DlgDC;
	HBITMAP hBitmap = CreateCompatibleBitmap(DlgDC,Width-DepcWidth,Height-DepcHeight);//Will Create Bitmap Comatible With Our Window
	SelectObject(MemDC,hBitmap);

	BitBlt(MemDC,0,0,Width,Height,ActWndDC,xSrc,ySrc,SRCCOPY);//Will Copy the Window into MemDC
	//BitBlt(DeskDC,110,110,Width,Height,MemDC,Begpt.x,Begpt.y,SRCCOPY);

	SaveBitmap(MemDC, hBitmap,"Sample.bmp");	// will Save DC into .bmp File	
	//Will Show u the .bmp File in MSPAINT.


}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DrawBox Will draw the Rectangle when you will Leave the mouse left Button in Selected Area Through Mouse.



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SavBitmap will Save our DC into .Bmp(sample.bmp) File.


BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,char* szPath)
{
	//OutputDebugString(L"Start SaveBitmap() ");
	DWORD error;
	FILE * fp= NULL;
	fp = fopen(szPath,"wb");
	if(fp == NULL)
	{
		//OutputDebugString(L"Error Unable to Create File ");
		return false;
	}
	BITMAP Bm;
	BITMAPINFO BitInfo;
	ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
	BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	BitInfo.bmiHeader.biBitCount = 0;

	if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
	{
	//	OutputDebugString(L"Error GetDIBits Fail");

		return (false);
	}
	Bm.bmHeight = BitInfo.bmiHeader.biHeight;
	Bm.bmWidth  = BitInfo.bmiHeader.biWidth;

	BITMAPFILEHEADER    BmHdr;

	BmHdr.bfType        = 0x4d42;   // 'BM' WINDOWS_BITMAP_SIGNATURE
	BmHdr.bfSize        = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight) 
		+ sizeof(BITMAPFILEHEADER) 
		+ sizeof(BITMAPINFOHEADER);
	BmHdr.bfReserved1    = BmHdr.bfReserved2 = 0;
	BmHdr.bfOffBits      = (DWORD) sizeof(BITMAPFILEHEADER) 
		+ sizeof(BITMAPINFOHEADER);

	BitInfo.bmiHeader.biCompression = 0;
	// Writing Bitmap File Header ////
	size_t size = fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);
	if(size < 1)
	{
		//OutputDebugString(L"Error  Header Write");
		error = GetLastError();
	}
	size = fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
	if(size < 1)
	{		
		//OutputDebugString(L"Error  Write");
		error = GetLastError();
	}
	BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
	if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight, 
		pData, &BitInfo, DIB_RGB_COLORS))
		return (false);
	if(pData != NULL)
		fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);

	fclose(fp);
	delete (pData);

	return (true);
}

WHATS WRONG
Last edited on
I don't have enough time to look very closely at that ATM, but you are varying from the stuff I linked to you pretty significantly.

Also, you are leaking resources.

Try to figure out exactly where the error is happening. You are not checking the return values of your calls to the WinAPI functions like you should. Do that and throw in some calls to GetLastError() to narrow down where something is going wrong.

That's half the battle.


BTW, I notice that you are trying to copy from the ActWinDC directly. That might not work like you think it will... though I could be mistaken (it's been a while since I've tried to do something like that).
Hey Zin Byte,
Duoas is right, the tutorial you are using is teaching bad practices, Definitely look into the MSDN link that Duoas mentioned, it shows how to copy the screen then display it inside your window as well as saving to a bitmap.bmp file.
The call to CreateCompatibleBitmap on line 30 does looks a bit suspicious, as you're passing in an uninitialized variable DlgDC for the DC handle.

Andy
Topic archived. No new replies allowed.