Experience Sharing with GDI [original]

Source: Internet
Author: User

For programming on the Windows series, GDI is a very important technical point. Many programs run abnormally many times, except for the well-known memory leakage, the leakage of GDI resources is also a direct cause. today, I will share some of my experiences in programming with you.
1. For the created GDI object, you must use deleteobject to release it. The release sequence is create and then release, and then create.
Here, create refers to the GDI function that starts with it, such as createdibitmap and createfont, and finally call deleteobject to release it.

2. The DC created in "CREATE" should be released using deletedc, and the obtained DC should be released using releasedc.

3. Make sure that each GDI object in the DC is not created by yourself when the DC is released; Make sure that each GDI object is not selected by any DC when it is released.
If we want to use the GDI function for drawing, the correct steps should be as follows:
A. Create a memory-compatible DC (createcompatibledc)
B. Create a memory-compatible Bitmap (createcompatiblebitmap)
C. The associated memory is compatible with DC and bitmap (SelectObject)
D. Drawing
E. bitblt to Target DC
F. Disconnect memory compatibility between DC and bitmap (SelectObject)
G. Destroy memory compatible with bitmap
H. Destroy memory compatible DC
Because SelectObject returns an original GDI object (if successful) when selecting a new GDI object, you need to save the returned value in Step C, it is used as an entry parameter in step f. in addition, steps G and H can be in random order, because they have no relationship at the moment, but to make the structure clear, I suggest you follow "create first and then release, after the create release first "principle.
Step F may be controversial, because even if this step is omitted, steps G and H still seem to return a successful value. but in fact it may not be executed successfully. At least boundschecker reports an error. The error message is generally that when the DC is released, it also contains a non-default GDI object, when the GDI object is released, the GDI object is still used by a DC. therefore, we recommend that you keep step f.

4. About using createcompatiblebitmap under 98
According to msdn, the size created cannot exceed 16 Mb. Is that true? Not the same ~! From my own test results (win98se-sc), this value is between 2044*2043 and 2044*2044, however, it will not work on another 98 system, then I simply set the limit to 2000*2000. fortunately, there is no problem yet, but I cannot ensure that this number is correct. another point is that if the width or height is greater than 32768, even if the other value is 1, the creation will fail. If you are interested, you can perform a test on your own. if you want to ensure that this function will always succeed under 98, try the following code:
Float factor = 10.f;
While (! Bitmap. createcompatiblebitmap (& DC, nwidth * factor, nheight * factor ))
{
Factor-= 0.01f;
}
In this way, at least the width and height are proportional :)

5. About using bitblt on the printer
Sometimes the diagram is completed in the memory-compatible DC, but bitblt fails to be used. at this time, first confirm whether the created memory is compatible with the DC and Bitmap Using the printer's DC. If the confirmation is correct, or if bitblt execution fails, then 80% may be because the memory compatibility with bitmap is too large, try again as follows:
Create another memory compatible DC2 and a relatively small memory compatible biimap2, which is about 1000*1000, I used it in this way: divide the content in the DC into blocks (1000*1000), place each bitblt on DC2, and then use bitblt in DC2 to print the DC. some people may have this question: why don't bitblt in the DC be directly transferred to the printer several times? Is there a difference? The answer is yes. If the bitmap in the DC is too large, even if you want bitblt to reach the printer in a 10*10 area, it will fail.

<End>

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.