// ** 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