C screen capture code

Source: Internet
Author: User
Void capturescreen ()
...{
// Obtain the screen width
Int nscreenwidth = getsystemmetrics (sm_cxscreen );
// Obtain the screen height
Int nscreenheight = getsystemmetrics (sm_cyscreen );
// Obtain the desktop window handle
Hwnd hsf-topwnd = getw.topwindow ();
// Obtain the desktop window device Environment
HDC hsf-topdc = getdc (hsf-topwnd );
// Generate the compatible device environment of the full screen device description table
HDC hcapturedc = createcompatibledc (hsf-topdc );
// Generate a compatible bitmap for the full screen device description table
Hbitmap hcapturebitmap = createcompatiblebitmap (hsf-topdc, nscreenwidth, nscreenheight );
// Select the compatible bitmap into the compatible device Environment
SelectObject (hcapturedc, hcapturebitmap );
// Copy the pixel data of the full screen window bitmap to the compatible device description table.
Bitblt (hcapturedc, 0, 0, nscreenwidth, nscreenheight, hsf-topdc, 0, 0, srccopy );

Bitmapinfo Bi;
Void * pbits = NULL;
Zeromemory (& BI, sizeof (BI ));
Bi. bmiheader. bisize = sizeof (Bi. bmiheader );
Bi. bmiheader. biheight = nscreenheight;
Bi. bmiheader. biwidth = nscreenwidth;
Bi. bmiheader. biplanes = 1;
Bi. bmiheader. bibitcount = 24;
Bi. bmiheader. bicompression = bi_rgb;
Bi. bmiheader. bisizeimage = 3 * nscreenheight * nscreenwidth;
// Generate a bitmap-compatible device description table
HDC hbmpfiledc = createcompatibledc (hsf-topdc );
// Generate Bitmap-compatible bitmap
Hbitmap hbmpfilebitmap = createdibsection (hsf-topdc, & BI, dib_rgb_colors, & pbits, null, 0 );
// Select the compatible bitmap and the device description table.
SelectObject (hbmpfiledc, hbmpfilebitmap );
// Copy the captured bitmap pixels to the device environment of the image.
Bitblt (hbmpfiledc, nscreenwidth, nscreenheight, hcapturedc, srccopy );
// Create a file
Handle hfile = createfile ("111.bmp", generic_write, file_pai_write, null, create_always, file_attribute_normal, null );
// Save the bitmap file
If (hfile! = Invalid_handle_value)
...{
DWORD dwret = 0;
Bitmapfileheader bmfheader;
Zeromemory (& bmfheader, sizeof (bmfheader ));
Bmfheader. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader );
Bmfheader. bfsize = Bi. bmiheader. bisizeimage + bmfheader. bfoffbits;
Bmfheader. bftype = 'mb ';
// Write the Bitmap header.
Writefile (hfile, & bmfheader, sizeof (bmfheader), & dwret, null );
// Write bitmap size, color table, and other information
Writefile (hfile, & BI. bmiheader, sizeof (Bi. bmiheader), & dwret, null );
// Write bitmap data blocks
Writefile (hfile, pbits, Bi. bmiheader. bisizeimage, & dwret, null );
Closehandle (hfile );
}

Deletedc (hbmpfiledc );
Deleteobject (hbmpfilebitmap );
Releasedc (hsf-topwnd, hsf-topdc );
Deletedc (hcapturedc );
Deleteobject (hcapturebitmap );
}

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.