taking snapshot of a window

Jul 3, 2013 at 6:45am
my friend has a program in VC++ 6.
He wants to take snapshot of the window except the title bar when a button is clicked. And also take a snapshot the window using specified coordinates( e.g. Top,left, bottom, right co-ordinates)
Is there any API that can do that. I dont have any idea about that, but maybe the CDC may contain the API.
Last edited on Jul 3, 2013 at 3:03pm
Jul 4, 2013 at 1:55am
There isn't a single function which does what you want, but you can use the GDI API, the DirectX API, and Windows Media API to do screen captures. The GDI approach is the simplest of these.

You will need to find the window you want to capture (unless you want to capture your own window?) and use that rather than the desktop window returned by GetDesktopWindow(). You also need to use the right window's dimension in the call to BitBlt (which can be adjusted to capture just a rectangular region of the window.)

Various methods for capturing the screen
http://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen

The example code use raw Win32. CDC wraps an HDC, so all the calls taking an HDC map to calls using an instance of CDC in MFC.

Andy

If a screencap tool would make more sense, then e.g.

Greenshot
http://getgreenshot.org/

Which can

Quickly create screenshots of a selected region, window or fullscreen; you can even capture complete (scrolling) web pages from Internet Explorer.

Google "screencap tool for windows" for more of the same.
Last edited on Jul 4, 2013 at 2:50pm
Jul 4, 2013 at 6:51pm
I visited the site http://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen. The stuff is pretty good, but it has methods to take snashot of the entire window. I couldn't find anything that would save only a particluar area in the window. Also where is the bitmap created by the CreateCompatibleBitmap() stored and how do i retrieve it ?
Jul 4, 2013 at 11:22pm
I couldn't find anything that would save only a particluar area in the window

As I stated in my last post, you just need to use the app window, rather than the desktop window.

I couldn't find anything that would save only a particluar area in the window

As I also stated in my last post, you adjust the parameters of the BitBlt call

Also where is the bitmap created by the CreateCompatibleBitmap() stored and how do i retrieve it ?

The code I pointed you at save the bitmap to file. If you can't follow the file save routine, then maybe you're trying to code something that is a bit beyond you?

The code does not exactly what you want, but could be used as the basic for code that does.

Andy
Last edited on Jul 4, 2013 at 11:23pm
Jul 5, 2013 at 5:50pm
closed account (G309216C)
Hi,

I found a snippet from a website about taking a snapshot, I do think with few small editing it will be useful for your purpose.
If you do not understand the code I suggest you learn before you attempt that.

CODE:
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
#include<Windows.h>

void ScreenShot(char*BmpName)
{
	HWND DesktopHwnd = GetDesktopWindow();
	RECT DesktopParams;
	HDC DevC = GetDC(DesktopHwnd);
	GetWindowRect(DesktopHwnd,&DesktopParams);
	DWORD Width = DesktopParams.right - DesktopParams.left;
	DWORD Height = DesktopParams.bottom - DesktopParams.top;

	DWORD FileSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+(sizeof(RGBTRIPLE)+1*(Width*Height*4));
	char *BmpFileData = (char*)GlobalAlloc(0x0040,FileSize);

	PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
	PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

	BFileHeader->bfType = 0x4D42; // BM
	BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
	BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

	BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
	BInfoHeader->biPlanes = 1;
	BInfoHeader->biBitCount = 24;
	BInfoHeader->biCompression = BI_RGB;
	BInfoHeader->biHeight = Height;
	BInfoHeader->biWidth = Width;

	RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	RGBTRIPLE color;
	
	HDC CaptureDC = CreateCompatibleDC(DevC);
	HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC,Width,Height);
	SelectObject(CaptureDC,CaptureBitmap);
	BitBlt(CaptureDC,0,0,Width,Height,DevC,0,0,SRCCOPY|CAPTUREBLT);
	GetDIBits(CaptureDC,CaptureBitmap,0,Height,Image,(LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);

	DWORD Junk;
	HANDLE FH = CreateFileA(BmpName,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,0,0);
	WriteFile(FH,BmpFileData,FileSize,&Junk,0);
	CloseHandle(FH);
        GlobalFree(BmpFileData); 
}

int main()
{
	ScreenShot("Hello.bmp");
}


GL
Jul 6, 2013 at 8:11am
Dear andy, do i have to replace the GetDesktopWindow by GetWindow and use its DC to blit the bits ? This is the code from the website BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code //here to save the captured image to disk. Can u tell me where to find the code to save the .bmp to disk. I could successfully blit the bits, but i am unable to save them on a disk
Last edited on Jul 6, 2013 at 8:13am
Jul 6, 2013 at 9:23am
do i have to replace the GetDesktopWindow by GetWindow and use its DC to blit the bits ?

Yes, or FindWindows, etc. depending on which window you want to get.

Can u tell me where to find the code to save the .bmp to disk

Ermm... In SpaceWorm's post, three above this one. Are you not bothering to read the replies??

Andy
Last edited on Jul 7, 2013 at 3:51pm
Jul 7, 2013 at 11:11am
my mistake, i didnt read the reply by spaceworm. Thank you for your help.
Jul 7, 2013 at 1:58pm
closed account (EwCjE3v7)
snipping tool..duh
Topic archived. No new replies allowed.