An atypical misuse of gdiplus

Source: Internet
Author: User
// ** Initialize GDI + gdiplus: gdiplusstartupinput; // ** this member variable is used to save the GDI + ID in the application after the GDI + is initialized. Ulong_ptr gdiplustoken; gdiplus: gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null); image (ImagePath); // load image graphics imagegraphics (& image ); // create a drawing handle through the image object and use this handle to operate imagegraphics on the image. settextrenderinghint (optional); wchar string [] = l "wishful thinking"; // The fontfamily (L "calibri"); gdiplus: font myfont (& fontfamily, 6, fontstyleregular, unitpoint); // The second is the font size // solidbrush blackbrush (color (255, 0, 0, 0 )); // translucent + text rgb color solidbrush whitebrush (color (255,255,255,255); // translucent + text rgb color pointf school_site (real) 300, (real) 100 ); // The pixel coordinate stringformat format; format. setalignment (stringalignmentnear); // text arrangement, that is, the center, left, and right of the corresponding position // wcscpy_s (string, ct2cw (STR); // if you use the cstring in MFC, you need to convert it to wcharimagegraphics. drawstring (string, wcslen (string), & myfont, school_site, & format, & whitebrush); // draw the string to the graph CLSID pngclsid; getencoderclsid (L "image/JPEG ", & pngclsid); ImagePath. replace (_ T (". jpg "), _ T (" -note.jpg "); image. save (ImagePath, & pngclsid, null); // Save the image with Chinese characters added. // ** uninstall GDI + gdiplus: gdiplusshutdown (gdiplustoken );

First look at this typical GDI + code, it seems that there is no problem. Compile the code in a main function to run it correctly. However, if the code is called in a function, it will crash.

After tracking, bugs can be easily found during debugging, but they are not noticed at ordinary times. Therefore, it is necessary to record the bug so that it will not fall into the same trap next time.

Note: After the GDI + is uninstalled, the image object is not destructed. When the image object is invalid, the GDI + resource is used, but the GDI + has been uninstalled, so the program will crash.

Summary: for this type of class libraries that need to be loaded and used up for uninstallation, it is best to use C ++ for simple encapsulation and use the class features to manage resources.

It is very easy to encapsulate GDI +, so the code is not pasted.

An atypical misuse of gdiplus

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.