Use of CreateCompatibleBitmap

Source: Internet
Author: User

function function: This function creates a compatible bitmap for devices that are related to the specified device environment.

Function prototypes: Hbitmap createcompatiblebitmap (HDC hdc,int nwidth,int nheight);

Parameters:

HDC: Device environment handle.

Nwidth: Refers to the width of the map, measured in pixels.

Nheight: Refers to the height of the map, measured in pixels.

Return value: If the function executes successfully, the return value is the handle to the bitmap, and if the function fails, the return value is null. If you want to get more error information, please call GetLastError.

Note: The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the parameter hdc. The bitmap can be selected in any memory device environment compatible with the original device. Both color and monochrome bitmaps are allowed because of the memory device environment. Therefore, when the specified device environment is a memory device environment, the bitmap format returned by the CreateCompatibleBitmap function is not necessarily the same. Compatible bitmaps created for non-memory device environments, however, typically have the same color format and use the same color palette as the specified device environment.

Quick check: Windows nt:3.1 and above, windows:95 and above, Windows ce:1.0 and above; header file: wingdi.h; library file: Gdi32.lib.

  PS: Need to be used in conjunction with CREATECOMPATIBLEDC

The physical HDC device will have memory and other resources on the bottom, but the compatible DC does not give the image pixels a space, so the compatible DC is always used in conjunction with the bitmap, so that the compatible DC can use the bitmap image pixel data space to provide itself with memory space similar to graphics.

This has many advantages, since we can take advantage of the various drawing functions of DC in the picture after loading the picture. See the example below:

Compatible DC at the beginning of the establishment, only 1*1 pixel size, SelectObject select bitmap after the drawing can be done.

The visible area of the memory DC is a simple area, unlike the visible areas of a physical DC that may be overwritten by other windows to produce a complex visible area. Because any drawing in the DC needs to consider drawing within the visible area, Never exceed the range of the visible area. Therefore, each GDI drawing output eventually needs to be trimmed out for each mega-region that makes up a complex visible area, so the physical DC will have a slower drawing effect than a compatible DC. This is one reason why we often use a compatible DC for dual-cache output


HDC HDC=GETDC (HWND);
HDC MEMDC=CREATECOMPATIBLEDC (HDC);
RECT RC;
BITMAP bmp;
Hbitmap Holdbmp,hbmp=loadbitmap (Hinstdvbres,makeintresource (Idb_clock));//load bitmap from Resource
holdbmp= (HBITMAP) SelectObject (memdc,hbmp);//Here the hbmp bitmap is selected to a compatible DC MEMDC, after which the compatible DC has and
Hbmp the same size of the drawing area, note that the GDI output returned by the bitmap is not valid.
GetObject (Hbmp,sizeof (BITMAP), &bmp);//This Gets the size information of the bitmap, which in fact is a range of compatible DC drawing output
SetRect (&rc,0,0,bmp.bmwidth,bmp.bmheight);
DrawText (MEMDC, "Center line Text" -1,&rc,dt_vcenter| dt_singleline| Dt_center);//output string in a compatible DC intermediate position
In this way we are equivalent to the hbmp this bitmap with the text callout, we can add the text callout of the bitmap to save. A simple image processing basic is OK.
SelectObject (memdc,holdbmp);//restore compatible DC data.
DeleteDC (MEMDC);


***********************************************************

The first phenomenon is this:
My program suddenly one day, a create out of the window to draw something, opened the skylight! With a look inside, the call to Cbitmap::createbitmap failed (the return value is false). The reason for using GetLastError is "Error_not_enough_memory".
Strange, this view code I did not move Ah! I tried to change it into a cbitmap::createcompatiblebitmap, okay! Forget it, whatever it is, just do it ~
Who knows, after some time, other windows open the skylight again!
This time I changed the CreateBitmap to CreateCompatibleBitmap, and not every time, it will create a failure!

Check out MSDN, about the CreateBitmap API has the following passage:
The CreateBitmap function can be used to create color bitmaps.
However, for performance reasons applications should use CreateBitmap to create monochrome bitmaps and CreateCompatibleBitmap to create color bitmaps.
Whenever a color bitmap returned from CreateBitmap are selected into a device context, the Syst EM checks that the bitmap matches the format of the device context it's being selected in To.
Because CreateCompatibleBitmap takes a device context, it returns a bitmap that have the same F Ormat as the specified device context.
Thus, subsequent calls to SelectObject is faster with a color bitmap from CreateCompatibleBitmap than with a color bitmap returned from CreateBitmap.

This passage looks quite around, saying that the CreateCompatibleBitmap API is more efficient than the CreateBitmap API. So, one of my questions is:
Are these two properties of CBitmap, like APIs, CreateCompatibleBitmap more efficient than CreateBitmap??

My question two: search, in my program, there are more than 20 places (17 windows) call createbitmap,10 Place call CreateCompatibleBitmap, and these occupy bitmap window only at the time of the last exit to destroy. Experienced colleagues to see, call the place is not too much? If you change to CreateCompatibleBitmap, will the resource usage be changed?

——————————————————————————

Before create, there is code similar to the following:
if (NULL! = m_pmembitmap)
{
M_pmembitmap->deleteobject ();
Delete M_pmembitmap;
}
M_pmembitmap = new CBitmap;
BOOL B = M_pmembitmap->createcompatiblebitmap (Pdc,rcclient.width (), Rcclient.height ());
It feels like there's nothing to leak.

==================>

Dizzy, that's what it is.
if (NULL! = m_pmembitmap)
{
M_pmembitmap->deleteobject ();
Delete M_pmembitmap;
M_pmembitmap = null;//Light Delete, M_pmembitmap is not null
}

This digest is from: http://fengqing888.blog.163.com/blog/static/3301141620091019104353119/, because the talk is really good, so I can not help sticking it over. Thanks for the dude .

Use of CreateCompatibleBitmap

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.