MFC drawing Summary-screen screenshot to obtain DIB/DDB graphics Pixel and dibddb

Source: Internet
Author: User

MFC drawing Summary-screen screenshot to obtain DIB/DDB graphics Pixel and dibddb
1. Copy the Screen of the current Screen to obtain BITMAP

To Copy the current screen, you must obtain the HDC of the current screen,

One is to directly capture the original image from the DC of the screen.

One is to capture images using compatible MemDC, and then attach the image processing, such as adjusting the size, or drawing the mouse.

(Note: If you Copy the screen of the current program, use this-> getsafehwnd () to pass in the GetDC parameter ())

(Note: If you Copy the screen to a specified window, you can use Spy ++ to obtain the Windows HWND and then pass in GetDC)

 

The method of capturing the original graph from a program is relatively simple, for example:

// Obtain the current screen HDCHDC hScreenDC =: GetDC (NULL); // directly obtain the BITMAPHBITMAP hBmp = (HBITMAP) GetCurrentObject (hScreen, OBJ_BITMAP) corresponding to the current DC );

An example of using compatible DC for image capturing is as follows:

// Obtain the current screen HDCHDC hScreenDC =: GetDC (NULL); // create compatible HDCHDC hMemDC =: CreateCompatibleDC (hScreenDC) // create compatible bitmap HBITMAP hbm = CreateCompatibleBitmap (hScreenDC, width, height); // select for MemDC compatible graph HBITMAP oldbm = (HBITMAP) SelectObject (hMemDC, hbm); // copy the image pixel content BitBlt (hMemDC, 0, 0, width, height, hScreenDC, left, top, SRCCOPY); // if you need to Draw the mouse information {// Draw the CursorICONINFO iconinfo; BOOL ret; ret = GetIconInfo (hcur, & iconinfo); if (ret) {xPoint. x-= iconinfo. xHotspot; xPoint. y-= iconinfo. yHotspot; // need to delete the hbmMask and hbmColor bitmaps // otherwise the program will crash after a while after running out of resourceif (iconinfo. hbmMask) DeleteObject (iconinfo. hbmMask); if (iconinfo. hbmColor) DeleteObject (iconinfo. hbmColor) ;}: DrawIcon (hMemDC, xPoint. x, xPoint. y, hcur);} // You can bind the bitmap SelectObject (hMemDC, oldbm) to the original DC );

 

2. Obtain the DDB image Pixel

DDB-Device dependency bitmap Device dependency Graph

Because Bitmap is obtained through DC, It is a DDB image.

Use the CBitmap function GetBitmapBits to obtain the relevant bits content, or use GetBitmap to obtain the entire BITMAP structure information.

However, the obtained pixel information is related to the current device. For example, if the device is a 24-bit, 32-bit, or bottom-up device, use the color mask, the obtained pixel is also like this,

Note: Here, CBitmap is a DDB image and CImage cannot be used for CImage: GetBits to obtain Pixel. For details, see CImage: IsDIBSection. To Save the image, you can use CImage: Save to Save the image.

 

For example:

// Bind hbmpCBitmap bitmap; Bitmap. attach (hBmp); // apply for memory byte pBuffer = new [width * 4 * height]; // obtain bits information Bitmap. getBitmapBits (pBuffer );

 

 

3. Obtain the DIB image Pixel

DIB-Device Independency Bitmap Device-independent graphics

This type of image contains the color digits, color table, and color mask of the image. Therefore, you can query the information from the Social reserve, which is also called a device-independent image.

GetDIBits is a common function used to obtain DIB graphs from HBitmap.

GetDIBBits can be used to query the structure information of the current device, and fill in the DIB header information BITMAPINFO description;

On the other hand, you can specify the BITMAPINFO/image format to convert the image information in the specified format from Bitmap.

Therefore, it can be used in two ways:

1. One is to get the format in the Device, then fill in BItmapInfo, and then obtain Pixel information based on the BitmapInfo.

2. Specify Bitmapinfo directly to obtain Pixsel information after Conversion Based on the specified format.

 

For example, if we consider both of the two methods, first obtain the device BitmapInfo, then modify the format, and then obtain the modified image 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 uses 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 the Pixel data lpBitmapInfo-> bmiHeader. biCompression = BI_RGB; lpBitmapInfo-> bmiHeader. biBitCount = 24; // query the bitmap DIB information GetDIBits (hMemDC, (HBITMAP) cBitmap. getSafeHandle (), 0, nHeight, pBuffer, lpBitmapInfo, DIB_RGB_COLORS) ;}// delete the header information after processing. lpBitmapInfo;


 


How can I use MFC to write a program to obtain the entire screen and save it in bmp format?

The code is probably like this. There are several key functions. If you need the complete code, leave a message.
CDC memDC;
CBitmap memBitmap, * pOldBitmap;
CWindowDC dc (GetDesktopWindow ());
MemDC. CreateCompatibleDC (& dc );
Int nWidth, nHeight;
/* NWidth = GetSystemMetrics (SM_CXSCREEN );
NHeight = GetSystemMetrics (SM_CYSCREEN );*/
CRect rect;
GetWindowRect topwindow ()-> GetWindowRect (rect );
ClientToScreen (& rect );
NWidth = rect. Width ();
NHeight = rect. Height ();
MemBitmap. CreateCompatibleBitmap (& dc, nWidth, nHeight );
POldBitmap = memDC. SelectObject (& memBitmap );
// MemDC. StretchBlt (, rect. Width (), rect. Height (), & dc, rect. Width (), rect. Height (), SRCCOPY );
MemDC. BitBlt (0, 0, nWidth, nHeight, & dc, 0, SRCCOPY );

BITMAPFILEHEADER BMFhead;
BITMAPINFOHEADER BMIhead;
BMFhead. bfReserved1 = 0;
BMFhead. bfReserved2 = 0;
BMFhead. bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER );
BMFhead. bfSize = BMFhead. bfOffBits + nWidth * nHeight * 4;
BMFhead. bfType = 0x4d42;

BMIhead. biBitCount = 32;
BMIhead. biClrImportant = 0;
BMIhead. biClrUsed = 0;
BMIhead. biSizeImage = 0;
BMIhead. biXPelsPerMeter = 0;
BMIhead. biYPelsPerMeter = 0;
BMIhead. biCompression = BI_RGB;
BMIhead. biHeight = nHeight;
BMIhead. biPlanes = 1;
BMIhead. biSize = sizeof (BITMAPINFOHEADER );
BMIhead. biWidth = nWidth;

DWORD dwSize = nWidth * nHeight * 4;
BYTE * pData = new BYTE [dwSize];
GetDIBits (memDC. m_hDC, (HBITMAP) memBitmap. m_hObject, 0, nHeight, pData, (LPBITMAPINFO) & BMIhead, DIB_RGB_COLORS );

CFile file;
File. Open ("test.bmp", CFile: modeCreate | CFile: modeWrite );
File. Write (& BMFhead, sizeof (BITMAPFILEHEADER ));
File. Writ ...... remaining full text>

Screen monitoring mirror?

The capture of computer screen images is a key technique in screen recording, Remote Computer Control, and multimedia teaching software. Based on the Windows operating system, there are multiple screen capture methods, the research focuses on how to quickly and effectively intercept screen image data in DBI (Device-Independent Bitmap) format. Currently, the popular screenshot Technology of commercial software mainly adopts the Api Hook Technology. However, this technology still consumes a lot of time for screenshots, in this way, there are still many restrictions on the hardware used to run the software, and it is a non-standard technology, which is not recommended by Microsoft.

1. screenshot Technology

1.1 Use api hook Technology

Using the api hook Technology, screenshots are based on the following principle. Most screen images are drawn by calling the drawing functions in the user State gdi32.dll, if you use the api hook Technology to intercept all calls to these functions in the system, you can obtain the regional coordinates of the screen image refreshed or changed; then, use the api function bitblt to copy the ddb format bitmap of the refreshed or changed screen area to the memory, and then use the function getbits to convert the ddb bitmap to the dbi bitmap, compression, storage, or transmission.

This method only captures changes. In this way, each screenshot is a valid operation. Each time (except for the first time), only new or changed parts are intercepted, which fundamentally solves the problem of large data volumes. However, this technology still has some disadvantages: 1. the api function used in the actual screenshot captures ddb bitmap, which requires a format conversion and takes a large amount of time. 2. If the coordinates of the rectangle in the area of the screen are r1, r2 ,...... Rn arrived one after another, in order not to lose the information of screen changes, usually not to intercept one by one, but to merge the rectangular coordinates, so that we can intercept areas that have not changed, without increasing the time consumption of screenshots, in addition, redundant data is generated. 3. This technology does not support directdraw. Because applications may use the directdraw driver to directly manipulate the Display memory, hardware bit block transfer, and graphic operations such as hardware overlap and switching surface, therefore, the api hook Technology will be ineffective and cannot capture screen changes. The 4api hook technology is used in screen word extraction, remote control, and multimedia teaching. However, this technology is a non-standard technology, which is not recommended by Microsoft.

1.2 use graphics-driven technology

Principle of this technology: The application calls the win32 gdi function to send a graphic output request, which is sent through the core mode gdi. In core mode, gdi sends these requests to the corresponding graphics driver. For example, display drivers and communication streams. The technology is described as follows:

(1) The display Driver outputs a series of Device Driver Interface DDI (Device Driver Interface) functions for GDI to call. Information is transmitted between the GDI and driver through the input/output parameters of these entry points.

(2) When the display driver is loaded, GDI calls the display driver function DrvEnableDriver. Here we provide the DDI function entry point supported by the display driver for the GDI to call, the image output function to be hooked.

(3) After GDI calls the DrvEnableDriver function and returns a successful result, GDI calls the DrvEnablePDEV function of the monitor driver. Here, you can set the display mode of the monitor and create a PDEV structure, the PDEV structure is the logical representation of the physical monitor.

(4) After the PDEV structure is successfully created, the display driver creates a surface for the Video hardware, which can be a standard DIB bitmap management surface, then the driver associates the surface with a PDEV structure so that all painting operations supported by the driver are performed on the DIB bitmap surface.

(5) When the application calls the drawing function in the user State GDI32.DLL to send a graphical request, the graphic engine sends the corresponding DDI function to the display driver, the display driver notifies the application of this graphic change event.

(6) ...... the remaining full text>
 

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.