MFC Drawing Summary-screen capture, get DIB/DDB graphics pixel

Source: Internet
Author: User

1. Copy screen to current screens, get bitmap

For the current screen Copy, you need to get the HDCfor the current screen,

One is to capture the original diagram directly from the screen DC .

One is then using a compatible MEMDC to capture the image, and then can attach graphics related processing, such as resizing, such as drawing the mouse up and so on.

( note : If it is on the current program Copy screen, the incoming GetDC parameter uses this->getsafehwnd ())

( note : If you specify the window Copy screen, you can use Spy + + to get the window HWND, and then pass in GetDC)

It is relatively simple to capture the original diagram from the program, for example :

Gets the current screen hdchdc HSCREENDC =:: GetDC (NULL);//Get directly the current DC corresponding bitmaphbitmap hbmp = (hbitmap) getcurrentobject (Hscreen, Obj_bit MAP);

Examples of using a compatible DC capture are as follows :

Get current screen Hdchdc HSCREENDC =:: GetDC (NULL);//Create compatible HDCHDC HMEMDC =:: CreateCompatibleDC (HSCREENDC)//Create compatible bitmap hbitmap hbm = Cre Atecompatiblebitmap (HSCREENDC, width, height);//for MEMDC check compatibility for figure hbitmap oldbm = (hbitmap) SelectObject (HMEMDC, HBM);// Copy Picture pixel content BitBlt (HMEMDC, 0, 0, width, height, hscreendc, left, top, srccopy); If you need to draw mouse information {//draw the cursoriconinfo iconinfo; BOOL ret;ret= geticoninfo (hcur,  &iconinfo); if (ret) {xpoint.x-= iconinfo.xhotspot;xpoint.y-= ICONINFO.YHOTSP Ot;//need to delete the hbmmask and Hbmcolor bitmaps//otherwise the program would crash after a while after running out of Resourceif (Iconinfo.hbmmask) DeleteObject (Iconinfo.hbmmask), if (Iconinfo.hbmcolor) DeleteObject (iconinfo.hbmColor );}::D Rawicon (HMEMDC,  xpoint.x,  xpoint.y, hcur);} Set back to the original DC binding location diagram SelectObject (HMEMDC,OLDBM);

2. Get DDB Graphics pixel

ddb-device dependency bitmap device dependent graphics

Because the current Bitmap are all obtained by DC , they are all DDB -type graphics.

Obtain the relevant bits content through the CBitmap function getbitmapbits, or you can use Getbitmap to get the entire BITMAP structure information.

but get to this pixel information is and current device related, for example device is 24 bit color /32 bit color / draw pixel is also the case,

Note : CBitmap here is DDB graphics and cannot be CImage with CImage ::GetBits get Pixel , see CImage:: isdibsection description. Also, if you want to save the graphic, you can use CImage: Save to save the graphic.

For example :

Binding Hbmpcbitmap bitmap; Bitmap.attach (hbmp);//application memory byte pbuffer = new [Width * 4 * height];//get bits information Bitmap. Getbitmapbits (pbuffer);

3. Get the DIB graphic pixel

dib-device independency Bitmap device-Independent graphics

This type of graph, which has its own structure with the graphic color of the number of bits / color table / Color Mask Description related information, so you can not rely on the information from the social preparation, also known as device-independent graphics.

The function commonly used getdibitsto get DIB graphics from hbitmap .

getdibbits can be used to query the structure information of the current device, fill in the DIB header information bitmapinfo description;

On the other hand, you can specify bitmapinfo/ to specify the graphic format and Convert the graphic information in the specified format from the Bitmap.

So there are two ways to use it :

1. One is to obtain the format in the Device , then fill in the bitmapinfo, and then based on the bitmapinfo, get Pixel Information

2. Another is to specify bitmapinfodirectly to get the Pixsel information after conversion based on the specified format

For example, if we consider both of these, we first get the device bitmapinfo, then modify the format, and then get the modified graphics Pixel

Initialize blank pbitmapinfolpbitmapinfo lpbitmapinfo = (bitmapinfo*) malloc (sizeof (bitmapinfo) +256 * sizeof (Rgbquad)); Memset ( Lpbitmapinfo, 0, sizeof (bitmapinfo) +256 * sizeof (Rgbquad)); lpbitmapinfo->bmiheader.bisize = sizeof (lpbitmapinfo- >bmiheader);//lpvbits use NULL to query bitmap DIB information if (GetDIBits (HMEMDC, hbmp, 0, 0, NULL, Lpbitmapinfo, dib_rgb_colors)) {         // Modify the format, use the modified format to obtain data pixel data lpbitmapinfo->bmiheader.bicompression = Bi_rgb;         Lpbitmapinfo->bmiheader.bibitcount =;         Query bitmap DIB information         getdibits (HMEMDC, (HBITMAP) cbitmap.getsafehandle (), 0, nheight, pbuffer, Lpbitmapinfo, dib_rgb_ COLORS);} Delete the header information after processing deleted lpbitmapinfo;


MFC Drawing Summary-screen capture, get DIB/DDB graphics pixel

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.