C ++ captures and saves screen images

Source: Internet
Author: User

The first step of screen capture technology is to capture the desktop of the screen.
Although the print screen key is acceptable, it is not applicable in practice.
The following code can be copied to a function and executed to generate a bmp file for obtaining the screen.
 
[Cpp]
// Obtain the CDC of the desktop window
CDC * pdeskdc = GetDesktopWindow ()-> GetDC ();
CRect re;
// Obtain the window size
GetClientRect (& re );
CBitmap bmp;
Bmp. CreateCompatibleBitmap (p1_dc, re. Width (), re. Height ());
// Create a compatible memory canvas
CDC memorydc;
Memorydc. CreateCompatibleDC (p1_dc );
 
// Select the paint brush
CBitmap * pold = memorydc. SelectObject (& bmp );
 
// Draw the image
Memorydc. BitBlt (0, 0, re. Width (), re. Height (), p1_dc, 0, 0, SRCCOPY );
 
// Obtain the mouse position and add the mouse Image
CPoint po;
GetCursorPos (& po );
HICON hinco = (HICON) GetCursor ();
Memorydc. DrawIcon (po. X-10, po. y-10, hinco );
// Select the original paint brush
Memorydc. SelectObject (pold );
BITMAP bit;
Bmp. GetBitmap (& bit );
/DWORD size = bit. bmWidth * bit. bmHeight;
 
// Define the image size (unit: byte)
DWORD size = bit. bmWidthBytes * bit. bmHeight;
LPSTR lpdata = (LPSTR) GlobalAlloc (GPTR, size );
 
// The file header is required to create a bmp file. For details, refer to the msdn
 
BITMAPINFOHEADER pbitinfo;
Pbitinfo. biBitCount = 24;
Pbitinfo. biClrImportant = 0;
Pbitinfo. biCompression = BI_RGB;
Pbitinfo. biHeight = bit. bmHeight;
Pbitinfo. biPlanes = 1;
Pbitinfo. biSize = sizeof (BITMAPINFOHEADER );
Pbitinfo. biSizeImage = size;
Pbitinfo. biWidth = bit. bmWidth;
Pbitinfo. biXPelsPerMeter = 0;
Pbitinfo. biYPelsPerMeter = 0;
 
GetDIBits (p1_dc-> m_hDC, bmp, 0, pbitinfo. biHeight, lpdata,
(BITMAPINFO *) & pbitinfo, DIB_RGB_COLORS );
 
BITMAPFILEHEADER bfh;
Bfh. bfReserved1 = bfh. bfReserved2 = 0;
Bfh. bfType = (WORD) ('M' <8) | 'B ');
Bfh. bfSize = 54 + size;
Bfh. bfOffBits = 54;
 
// Write a file
 
CFile file;
If (file. Open ("1.bmp", CFile: modeCreate | CFile: modeWrite ))
{
File. WriteHuge (& bfh, sizeof (BITMAPFILEHEADER ));
File. WriteHuge (& pbitinfo, sizeof (BITMAPINFOHEADER ));
File. WriteHuge (lpdata, size );
File. Close ();
}
GlobalFree (lpdata );

 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.