"VC + + Technology 006" Interception computer desktop and save it as a BMP picture

Source: Internet
Author: User
Tags bmp image

This article mainly describes how to intercept the computer desktop and save it as a BMP image.

1. bmp image File composition

BMP is a standard image file format in the Windows operating system.

BMP image files are made up of four parts:

(1) Bitmap header file data structure, including BMP image file type, file size and other information.

(2) Bitmap information data structure, including BMP image width, height, compression type and other information.

(3) Color table, this section optional, some bitmaps need, some bitmaps (such as 24 bit true color bitmap) do not need.

(4) Bitmap data.

1.1-bit Graph header file data structure

The bitmap header file data structure contains information such as the type of BMP image file, file size, and so on, taking up 14 bytes. Its structure is defined as follows:

1typedefstructTagbitmapfileheader {2WORD Bftype;//bitmap type (must be BM)3DWORD bfsize;//Bitmap Size4WORD bfReserved1;//bitmap reserved Words5WORD BfReserved2;//bitmap reserved Words6DWORD bfoffbits;//the starting position of the bitmap data7} Bitmapfileheader;

Where Bftype indicates the bitmap type, must be a bitmap reserved word for bm;bfreserved1 and bfReserved2, must be 0;bfoffbits to indicate the offset of the file header, and also the starting position of the graph data.

1.2-bit graph information data structure

The bitmap information data structure is used to describe information such as the size of the bitmap, which occupies 40 bytes. Its structure is defined as follows:

1typedefstructtagbitmapinfoheader{2DWORD bisize;//number of bytes occupied by this structure3LONG Biwidth;//Bitmap Width4LONG Biheight;//Bitmap Height5WORD biplanes;//number of bitmap palettes6WORD biBitCount;//the number of bits required per pixel7DWORD bicompression;//bitmap Compression type (0 not compressed)8DWORD biSizeImage;//Bitmap Size9LONG Bixpelspermeter;//Bitmap Horizontal ResolutionTenLONG Biypelspermeter;//Bitmap Vertical Resolution OneDWORD biclrused;//The number of colors in the color table that the bitmap actually uses ADWORD biclrimportant;//number of important colors during bitmap display -} Bitmapinfoheader;

where Biwidth and Biheight each represent the width of the bitmap and the height of the bitmap, evenly pixels, biplanes represents the number of palettes, must be 1;bibitcount to represent the number of bits required per pixel, the value must be 1 (two-color), 4 (16-color), 8 (256 colors), 16 (high color), or 24 (true color); Bicompression represents a bitmap compression type whose value must be 0 (not compressed), 1 (bi_rle8 compressed), or 2 (bi_rle4 compression).

1.3 Color Table

A color table is used to describe a color in a bitmap, which has several table entries, each of which is a struct of type Rgbquad that defines a color. The RGBQUAD structure is defined as follows:

1 struct Tagrgbquad {2         BYTE    Rgbblue;                    // Blue 3         BYTE    Rgbgreen;                   // Green 4         BYTE    rgbred;                     // Red 5         BYTE    rgbreserved;                // Reserved Values 6 } Rgbquad;

The number of Rgbquad structure data in the color table is determined by biBitCount, when biBitCount is 1,4,8, there are 2,16,256 table entries, when biBitCount is 24, there are no color table entries.

1.4-bit Graph data

The bitmap data records each pixel value of the bitmap, from left to right within the scan line, and from bottom to top. When biBitCount is 1 o'clock, 8 pixels account for 1 bytes; When biBitCount is 4 o'clock, 2 pixels are 1 bytes; When biBitCount is 8 o'clock, 1 pixels are 1 bytes; When biBitCount is 24 o'clock, 1 pixels are 3 bytes (in order B, G, R).

2. Programming examples

The following code example demonstrates how to intercept a computer desktop and save it as a BMP image.

1 /*2 * Function Function: This function is used to intercept computer desktop and save it as BMP picture3 * Remark:4 * Author: Blog Park is still indifferent5  */6 voidCcopyscreendlg:: Copyscreentobitmap ()7 {8     //To Create a desktop device environment object9 CDC Screendc;TenScreendc.createdc ("DISPLAY", NULL, NULL, NULL); One      A     //Create a desktop bitmap object - CBitmap bmp; -     intnwidth = GetSystemMetrics (Sm_cxscreen);//Desktop Width the     intnheight = GetSystemMetrics (Sm_cyscreen);//Desktop Height -Bmp. CreateCompatibleBitmap (&Screendc, nwidth, nheight); -      -     //Create a Memory device environment object + CDC MEMDC; -Memdc.createcompatibledc (&SCREENDC); +Memdc.selectobject (&bmp); AMemdc.bitblt (0,0, nwidth, nheight, &AMP;SCREENDC,0,0, srccopy); at      -     //Populating bitmap Data - BITMAP BM; -Bmp. Getbitmap (&BM); -DWORD bmsize = bm.bmwidthbytes*bm.bmheight;//Bitmap Data Size -LPSTR Bmdata = (LPSTR) GlobalAlloc (gptr, bmsize);//Bitmap Data in  -     //Populate bitmap header file data structure body to Bitmapfileheader HDR; +Hdr.bftype = (WORD) ('M'<<8)|'B';//bitmap type (must be BM) -Hdr.bfsize = Wu+bmsize;//Bitmap Size theHdr.bfreserved1 =0;//bitmap reserved Words *Hdr.bfreserved2 =0;//bitmap reserved Words $Hdr.bfoffbits = Wu;//the starting position of the bitmap dataPanax Notoginseng  -     //populating bitmap information data structure body the Bitmapinfoheader Bmpinfohdr; +Bmpinfohdr.bisize =sizeof(Bitmapinfoheader);//number of bytes occupied by this structure ABmpinfohdr.biwidth = Bm.bmwidth;//Bitmap Width theBmpinfohdr.biheight = Bm.bmheight;//Bitmap Height +Bmpinfohdr.biplanes = Bm.bmplanes;//number of bitmap palettes -Bmpinfohdr.bibitcount = Bm.bmbitspixel;//the number of bits required per pixel $Bmpinfohdr.bicompression =0;//bitmap Compression type (0 not compressed) $Bmpinfohdr.bisizeimage = bmsize;//Bitmap Size -Bmpinfohdr.bixpelspermeter =0;//Bitmap Horizontal Resolution -Bmpinfohdr.biypelspermeter =0;//Bitmap Vertical Resolution thebmpinfohdr.biclrused =0;//The number of colors in the color table that the bitmap actually uses -Bmpinfohdr.biclrimportant =0;//number of important colors during bitmap displayWuyi  the     //specifies that the color table is composed of RGB three direct values -GetDIBits (SCREENDC, BMP,0, Bmpinfohdr.biheight, Bmdata, (bitmapinfo*) &Bmpinfohdr, dib_rgb_colors); Wu  -     //Save BMP Pictures About CFile file; $     if(file. Open ("temp.bmp", cfile::modecreate|cfile::modewrite)) -     { -File. Writehuge (&AMP;HDR,sizeof(Bitmapfileheader));//Write bitmap header file data -File. Writehuge (&AMP;BMPINFOHDR,sizeof(Bitmapinfoheader));//Write bitmap information data AFile. Writehuge (Bmdata, bmsize);//Write bitmap Data + file. Close (); the     } -}

"VC + + Technology 006" Interception computer desktop and save it as a BMP picture

Related Article

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.