Concepts of CDC and bitmap

Source: Internet
Author: User
[Switch] compatible with the CDC to save the entire screen program and further thoughts

// Hwnd getasktopwindow () returns the handle of the desktop window
CDC * pdeskdc = getdesktopwindow ()-> getdc (); // obtain the pointer (handle) of the context of the desktop window)

Crect rect;
Getclienttopwindow ()-> getclientrect (rect); // obtain the customer region of the desktop screen

CDC memdc; // defines a memory Context
// HDC createcompatibledc (HDC), which creates a memory device context environment compatible with the specified device
Memdc. createcompatibledc (p1_dc );

// Before an application can use the memory device context for plotting,
// It must select a bitmap with the correct height and width to the device context,
// You can use the createcompatiblebitmap function to specify the height, width, and color combinations to meet the function call needs.

Cbitmap BMP;
// Hbitmap createcompatiblebitmap (HDC, int nwidth, int nheight)
// This function creates a bitmap that is compatible with the specified device environment.

BMP. createcompatiblebitmap (p1_dc, rect. Width (), rect. Height (); // create a compatible bitmap and specify the width and height.

Memdc. SelectObject (& BMP); // select the bitmap into the memory context.

Bitmap bitmapinfo; // defines a bitmap structure, which defines the height, width, color format, and bit value of the logical bitmap.

BMP. getbitmap (& bitmapinfo); // This function is used to view information about a cbitmap object. The returned information is stored in the bitmap structure.

Int panelsize = 0; // record the color palette size
If (bitmapinfo. bmbitspixel <16) {// determines whether the color bitmap is true.

// If the number of colors used by the bitmap is not 16, the color palette size is bmbitspixel * sizeof (rgbquad)
Panelsize = POW (2, bitmapinfo. bmbitspixel * sizeof (rgbquad); // The rgbquad structure identifies the color data used by pixels

}
Bitmapinfo * pbinfo = (bitmapinfo *) localalloc (lptr, sizeof (bitmapinfo) + panelsize );
// The localalloc function is used to allocate local heap memory for data.
// Bitmapinfoheader structure in the bitmapinfo structure to save bitmap Information
// Fill the bitmap information Header

// The code of the header for filling bitmap information is omitted

// Copy the bitmap from the source device context to the current device context, that is, copy the bitmap from pdeskdc to memdc.
Memdc. bitblt (, bitmapinfo. bmwidth, bitmapinfo. bmheight, p1_dc, srccopy );

After I add a comment, I am going to send the code after my comment to this old man. He seems to be offline. This guy, I am going to open the notebook again, it's a waste of time for me to watch movies (haha, by the way, watching movies with PPStream is quite good). Just as I was about to continue watching movies, this old man appeared again. The following is our next conversation.

"One of my students raised a question about this code, and I was dizzy," he said ."

I asked, "which code has a problem ?"

He asked: "When is the screen saved in the BMP bitmap ?"

"The createcompatiblebitmap function, SelectObject function, and bitblt function are used together to save the data to the memory device context memdc ."

"Are the three functions working together? I have another question ."

I said, "Let's talk about the second question. Let's talk about it and let me see it together ."

"The. SelectObject function has put the bitmap BMP into the memory device context memdc. Why do we need to use the bitblt function to copy the bitmap BMP from pdeskdc to the memory device context mendc? Isn't it redundant this time ?"

Now I finally understand the cause of his problem, because this guy didn't really figure out how bitmap files are formed step by step. Explained that he finally woke up in the afternoon. In fact, these two questions should be regarded as one question and worth further discussion. Now I will explain this question to you.

First, let's talk about bitmap files. Bitmap files are composed of four parts:

1. Bitmap File Header (which can be described by the bitmapfileheader structure ).

2. Bitmap information header (which can be described by the bitmapinfoheader structure ).

3. Color Table (which can be described by rgbquad structure)

4. bitmap data blocks (Image Data byte arrays that follow the color table)

Let me talk about the functions of createcompatiblebitmap, SelectObject, and bitblt functions.

Bool createcompatiblebitmap (CDC * PDC, int nwidth, int nheight): initializes a bitmap that is compatible with the specified device context, this bitmap has the same color bit number or number of digits per pixel as the specified device context.

Cbitmap * SelectObject (cbitmap * pbitmap): selects the object into the device context.

Bool bitblt (int x, int y, int nwidth, int nheight, CDC * psrcdc, int xsrc, int ysrc, DWORD dwdrop): copy the bitmap from the context of the source device to the context of the current device.

After reading the functions of these three functions, the following explains the process of creating a bitmap file. First, when the createcompatiblebitmap function is called, the returned bitmap object BMP only contains the bitmap information header in the corresponding device description table, excluding the color table and bitmap data block. Then, call SelectObject to select the bitmap into the memory device context memdc, and call the bitblt function to copy the color table and bitmap data block from the source device to the memory device, with the bitmap part, you can store it as a bitmap file or combine it into a bitmap data stream.

It doesn't matter if you don't quite understand it, because it is a relatively low-level thing. Remember the following sequence when using compatible CDC to save graphics and screens.

Step 1: Use the getdesktopwindow ()-> getdc () function to create a display device context p1_dc, which corresponds to the entire display screen.

Step 2: Call createcompatiblebitmap to create a bitmap BMP that is compatible with the display device context.

Step 3: Call createcompatiblebitmap to create a memory device context memdc that is compatible with the display device context

Step 4: Use the SelectObject function to select the created bitmap into the memory device context

Step 5: Call the bitblt function to copy the area specified by the device context (that is, the specified screen area) to the context of the memory device.

Step 6: Obtain the bitmap sentence handle from the context of the memory device

The last concept is true color bitmap and color palette. The number of bitmaps we call. This "bit" represents the number of digits occupied by each pixel in the bitmap. There is such a part of code in our program:

Int panelsize = 0; // record the color palette size
If (bitmapinfo. bmbitspixel <16) {// determines whether the color bitmap is true.

// If the number of colors used by the bitmap is not 16, the color palette size is bmbitspixel * sizeof (rgbquad)
Panelsize = POW (2, bitmapinfo. bmbitspixel * sizeof (rgbquad); // The rgbquad structure identifies the color data used by pixels

}

I will explain that we usually convert a 16-bit or higher-16-Bit Bitmap into a true color bitmap, which does not require a color palette. Bitmap smaller than 16 requires a color palette. The reason is as follows: For a 16-Bit Bitmap, each pixel occupies two ways: 555 and 565:

555 mode: 0 rrrrrggggghhhhh

Method 565: rrrrrgggggghhhhh

A 16-Bit Bitmap, such as a 24-Bit Bitmap, already occupies three bytes, which can represent RGB, and there is no need to use the color palette. 32-bit occupies 4 bytes, which can also represent RGB, So 64 is the same.

Well, we have also extended a lot of things worth discussing from this question. Finally, we can explain that VC ++ 6.0 is difficult to discuss the image, because some concepts are very abstract, note.

Original: http://hi.baidu.com/lysygyy/item/fc26b9121acb62cd38cb3003

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.