MFC screenshot Function

Source: Internet
Author: User

Void cvideowndinst: toshotscreen (){

Crect VRC;

Mvideownd. getwindowrect (& VRC );

Savebitmaptofile (copyscreentobitmap (& VRC), "C: // 1.bmp ");


}


// Save the specified area of the screen as an image

Hbitmap cvideowndinst: copyscreentobitmap (lprect) // lprect indicates the selected region

{

HDC hscrdc, hmemdc;

// Screen and memory device description table

Hbitmap, holdbitmap;

// Bitmap handle

Int NX, NY, nx2, ny2;

// Coordinates of the selected region

Int nwidth, nheight;

// Bitmap width and height

Int xscrn, yscrn;

// Screen resolution

// Make sure that the selected area is not empty.

If (isrectempty (lprect ))

Return NULL;

// Create a device description table for the screen

Hscrdc = createdc ("display", null );

// Create a compatible memory device description table for the screen device description table

Hmemdc = createcompatibledc (hscrdc );

// Obtain the coordinates of the selected region

Nx = lprect-> left;

NY = lprect-> top;

Nx2 = lprect-> right;

Ny2 = lprect-> bottom;

// Obtain the screen resolution

Xscrn = getdevicecaps (hscrdc, horzres );

Yscrn = getdevicecaps (hscrdc, vertres );

// Make sure that the selected area is visible

If (nx <0)

Nx = 0;

If (NY <0)

NY = 0;

If (nx2> xscrn)

Nx2 = xscrn;

If (ny2> yscrn)

Ny2 = yscrn;

Nwidth = nx2-NX;

Nheight = ny2-NY;

// Create a bitmap compatible with the on-screen device description table

Hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight );

// Select the new bitmap to the memory device description table.

Holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap );

// Copy the screen device description table to the memory device description table.

Bitblt (hmemdc, 0, 0, nwidth, nheight, hscrdc, NX, NY, srccopy );

// Obtain the handle of the screen bitmap.

Hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap );

// Clear

Deletedc (hscrdc );

Deletedc (hmemdc );

// Returns the bitmap handle.

Return hbitmap;

}


// Savebitmaptofile Save the image as a file

Int cvideowndinst: savebitmaptofile (hbitmap, lpstr lpfilename) // hbitmap is the screen bitmap handle.

{// Lpfilename is the bitmap file name.

HDC;

// Device description

Int ibits;

// The number of bytes occupied by each pixel in the current display resolution

Word wbitcount;

// The number of bytes occupied by each pixel in the bitmap

// Defines the color palette size, pixel bytes in the bitmap, bitmap file size, and number of written file bytes.

DWORD dwpalettesize = 0, dwbmbitssize, dwdibsize, dwwritten;

Bitmap bitmap;

// Bitmap attribute Structure

Bitmapfileheader bmfhdr;

// Bitmap file header Structure

Bitmapinfoheader Bi;

// Bitmap header Structure

Lpbitmapinfoheader lpbi;

// Point to the bitmap information header Structure

Handle FH, hdib, hpal;

Hpalette holdpal = NULL;

// Defines the file, allocates memory handles, and color palette handles.


// Calculate the number of bytes occupied by each pixel in a bitmap file

HDC = createdc ("display", null );

Ibits = getdevicecaps (HDC, bitspixel )*

Getdevicecaps (HDC, PLANeS );

Deletedc (HDC );

If (ibits <= 1)

Wbitcount = 1;

Else if (ibits <= 4)

Wbitcount = 4;

Else if (ibits <= 8)

Wbitcount = 8;

Else if (ibits <= 24)

Wbitcount = 24;

Else

Wbitcount = 32;

// Calculate the palette size

If (wbitcount <= 8)

Dwpalettesize = (1 <wbitcount) * sizeof (rgbquad );


// Set the bitmap information header Structure

GetObject (hbitmap, sizeof (Bitmap), (lpstr) & Bitmap );

Bi. bisize = sizeof (bitmapinfoheader );

Bi. biwidth = bitmap. bmwidth;

Bi. biheight = bitmap. bmheight;

Bi. biplanes = 1;

Bi. bibitcount = wbitcount;

Bi. bicompression = bi_rgb;

Bi. bisizeimage = 0;

Bi. bixpelspermeter = 0;

Bi. biypelspermeter = 0;

Bi. biclrused = 0;

Bi. biclrimportant = 0;


Dwbmbitssize = (bitmap. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight;

// Allocate memory for bitmap content

/* XXXXXXXX calculate the bitmap size decomposition (explain the preceding statement) xxxxxxxxxxxxxxxxxxxx

// The number of bytes for each scan row should be an integer multiple of 4. The specific algorithm is as follows:

Int biwidth = (bitmap. bmwidth * wbitcount)/32;

If (bitmap. bmwidth * wbitcount) % 32)

Biwidth ++; // not an integer plus 1

Biwidth * = 4; // here, the calculated number of bytes for each scan row.

Dwbmbitssize = biwidth * bitmap. bmheight; // obtain the size.

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */


Hdib = globalalloc (ghnd, dwbmbitssize + dwpalettesize + sizeof (bitmapinfoheader ));

Lpbi = (lpbitmapinfoheader) globallock (hdib );

* Lpbi = Bi;

// Process the palette

Hpal = getstockobject (default_palette );

If (hpal)

{

HDC =: getdc (null );

Holdpal = selectpalette (HDC, (hpalette) hpal, false );

Realizepalette (HDC );

}

// Obtain the new pixel value under the color palette

Getdibits (HDC, hbitmap, 0, (uint) bitmap. bmheight, (lpstr) lpbi + sizeof (bitmapinfoheader) + dwpalettesize, (bitmapinfo *) lpbi, dib_rgb_colors );

// Restore the palette

If (holdpal)

{

Selectpalette (HDC, holdpal, true );

Realizepalette (HDC );

: Releasedc (null, HDC );

}

// Create a bitmap file

FH = createfile (lpfilename, generic_write, 0, null, create_always, file_attribute_normal | file_flag_sequential_scan, null );

If (FH = invalid_handle_value)

Return false;

// Set the bitmap file header

Bmfhdr. bftype = 0x4d42; // "BM"

Dwdibsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + dwpalettesize + dwbmbitssize;

Bmfhdr. bfsize = dwdibsize;

Bmfhdr. bfreserved1 = 0;

Bmfhdr. bfreserved2 = 0;

Bmfhdr. bfoffbits = (DWORD) sizeof (bitmapfileheader) + (DWORD) sizeof (bitmapinfoheader) + dwpalettesize;

// Write the bitmap file header

Writefile (FH, (lpstr) & bmfhdr, sizeof (bitmapfileheader), & dwwritten, null );

// Write the remaining contents of the bitmap file

Writefile (FH, (lpstr) lpbi, sizeof (bitmapinfoheader) + dwpalettesize + dwbmbitssize, & dwwritten, null );

// Clear

Globalunlock (hdib );

Globalfree (hdib );

Closehandle (FH );

Return true;

}


This article is from the blog "I know what I don't have". I don't want to repost it!

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.