Somebody needed to capture the desktop image?

closed account (3pj6b7Xj)
There was someone about 2 days ago that wanted to capture the desktop image and store it in a file. I said I would post my capture class to see if it would help them out, well here it is...please note, that is very old code and I wrote it during my first week of windows programming, there is probably a better way to do this.

It can capture the screen but it cannot store it in a file and I believe there is one function that I never worked on CopyClient() which was supposed to only capture the client window of the application hadle hWnd that you specified...

I put it all in 1 file to make things easier... leaving your implementation inside the header file is usually bad practice, that is normally how I do things and then I just separate the implementation into their appropriate .cpp files...blah blah.

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
114
115
116
117
118
119
120
121
// --- Capture.h ; Rafael Perez-Santana 
// I noticed that capturing the screen in win32/GDI required several steps!
// To avoid having to write all this code over and over again, I created this
// class to capture a screen section and display it again when required.
//
// To capture the screen use takepic() and specify the top, left, bottom, right
// co-ordinates of the screen to capture. You can re-display the captured image
// by copying the stored image into the device-context of another application
// by using the copyto() function.

#include <windows.h>

class Capture
{
	public:
	Capture(HWND hSethWnd); // class constructor
	~Capture(); // class destructor

	void TakePic(int top, int left, int bottom, int right);
	void CopyTo(HWND hwnd);
	void CopyClient();

	protected:
	HWND hWnd;
	HDC hdcScreen; // handle to device driver
	HDC hdcCompatible; // compatible dc to device driver handle
	HBITMAP hbmScreen; // bitmap object
	int Top; // top left vertical co-ordinate
	int Left; // top left horizontal co-ordinate
	int Bottom; // bottom right vertical co-ordinate
	int Right; // bottom right horizontal co-ordinate
	bool havedata; // states if we have captured the screen
};

// class constructor
Capture::Capture(HWND hSethWnd)
{
	hWnd = hSethWnd;
	havedata = false; // no image stored at start.
}

// class destructor
Capture::~Capture()
{
	// when application terminates, all of the allocated
	// resources are given back to the system by deleting them.

	DeleteObject(hbmScreen); // delete bitmap object
	DeleteDC(hdcScreen); // delete this device-context
	DeleteDC(hdcCompatible); // delete this one too
}

// copies the stored image to another application handle
void Capture::CopyTo(HWND hwnd)
{
	if (!havedata) return; // cannot proceed, we have nothing to copy!

	HDC screendata = GetDC(hwnd); // the area to copy to...

	// make the transfer using stored data
	BitBlt(	screendata,		// handle to destination device context 
			Left,			// x-coordinate of destination rectangle's upper-left corner
	    	Top,			// y-coordinate of destination rectangle's upper-left corner
	    	Right,			// width of destination rectangle 
	    	Bottom,			// height of destination rectangle 
	    	hdcCompatible,	// handle to source device context 
	    	Left,			// x-coordinate of source rectangle's upper-left corner  
	    	Top,			// y-coordinate of source rectangle's upper-left corner
	    	SRCCOPY 		// raster operation code 
	   		);

	ReleaseDC(hwnd,screendata); // release device-context
}

// captures the screen region specified in top, left, bottom & right
void Capture::TakePic(int top, int left, int bottom, int right)
{
	// initialize capture settings
	Top = top; Left = left; Bottom = bottom; Right = right; // save co-ordinates
	int width = right-left; // width for bitmap object
	int height = bottom; // height for bitmap object
	
	// create a device-context, this will be the source to copy from.
	hdcScreen = CreateDC( 	 
    	"DISPLAY",			// pointer to string specifying driver name 
	    NULL,				// pointer to string specifying device name 
	    NULL,				// do not use; set to NULL 
	    NULL 				// pointer to optional printer data 
		);

	// we need to create a CompatibleDC with 'hdcScreen' because
	// CreateCompatibleBitmap() only accepts this type of handle.
	hdcCompatible = CreateCompatibleDC(hdcScreen); 

	// now we define the handle to the compatible bitmap
	// and we point it to 'hdcScreen' where it is saved.
	hbmScreen =  CreateCompatibleBitmap(
    	hdcScreen,	// handle to device context 
	   	width,		// width of bitmap, in pixels  
	    height 		// height of bitmap, in pixels  
	   	);
 
	// The SelectObject() function selects an object into the specified device context.
	// The new object replaces the previous object of the same type.
 	SelectObject(hdcCompatible, hbmScreen); 

	// now we copy data from the source to our destination 'hdcCompatible'
	// which, is actually our handle to the bitmap object we created.
	BitBlt(	hdcCompatible,	// handle to destination device context 
	    	left,			// x-coordinate of destination rectangle's upper-left corner
		    top,			// y-coordinate of destination rectangle's upper-left corner
		    right,			// width of destination rectangle 
		    bottom,			// height of destination rectangle 
		    hdcScreen,		// handle to source device context 
	    	left,			// x-coordinate of source rectangle's upper-left corner  
	    	top,			// y-coordinate of source rectangle's upper-left corner
	    	SRCCOPY 		// raster operation code 
	   		);

	havedata = true; // we now have a stored image.
}


Almost forgot, an example of how to use this..

1
2
3
4
5
6
Capture mydesktop;
mydesktop.takepic(0,0,1024,768); // if the desktop was 1024x768

// and if you wanted to show the desktop inside your client area of the application...

mydesktop.copyto(hWnd); // Blits the image to the client area 


Do what ever you want with it...
Last edited on
You can use GetSystemMetrics(SM_CXSCREEN)/GetSystemMetrics(SM_CYSCREEN) to get the size of the screen.

http://msdn.microsoft.com/en-us/library/ms724385(v=vs.85).aspx
closed account (3pj6b7Xj)
I learned that long ago but thanks :)
Thanks man, I asked for it and I will check to see how it works.. ! :)
closed account (3pj6b7Xj)
It should work, you'll find my coding is rather simple. The part about saving the bitmap to a file, your on your own there, I haven't even begun messing with bitmaps yet, how to load them from a file, etc.
Topic archived. No new replies allowed.