C language version GDI + application example-watermark image creation

Source: Internet
Author: User
Tags transparent color

This article introduces an example of using the C language version of GDI + to create a watermark image. The following is the program code:

# Include ".. /.. /samplecode/comcode/application. H "</P> <p> # pragma hdrstop </P> <p> # defineid_save101 </P> <p> pgpbitmap bitmap; <br/> pgpbitmap photo; <br/> pgpbitmap watermark; </P> <p> int wmwidth, wmheight; <br/> int phwidth, phheight; </P> <p> hfont = NULL; </P> <p> colormatrix = {1.0, 0.0, 0.0, 0.0, 0.0, <br/> 0.0, 1.0, 0.0, 0.0, 0.0, <br/> 0.0, 0.0, 1.0, 0.0, 0.0, <br/> 0.0, 0.0, 0.0, 0.3, 0.0, <Br/> 0.0, 0.0, 0.0, 0.0 };</P> <p> void oncreate (void) <br/>{< br/> pgpimageattr ATTR; <br/> pgpfont font; <br/> pgpbrush brush; <br/> pgpstrformat format; <br/> pgpgraphics g; <br/> float X, Y; <br/> rect drect; <br/> rectf frect; <br/> hwnd hbutton; </P> <p> hfont = createfont (14, 0, 0, 0, 0, fw_normal, <br/> false, gb2312_charset, out_default_precis, <br/> clip_default_precis, default_quality, Ff_decorative, text (""); </P> <p> hbutton = createwindow (text ("button"), text ("save"), ws_child | ws_visible, <br/> 400,204, 80, 25, handle, (hmenu) id_save, hinstance, null); <br/> sendmessage (hbutton, wm_setfont, (wparam) (hfont ), 0); </P> <p> // read the original image <br/> photo = bitmapfromfile (L ".. //.. // media // 100_0349.jpg ", false); <br/> phwidth = imagegetwidth (photo); <br/> phheight = imagegetheight (photo ); </P> <p> // Read watermark image <br/> watermark = bitmapfromfile (L ".. //.. // media // watermark.bmp ", false); <br/> wmwidth = imagegetwidth (watermark); <br/> wmheight = imagegetheight (watermark ); </P> <p> // create a new bitmap with a resolution of 72 <br/> bitmap = bitmapcreate (phwidth, phheight, pixelformat32bppargb ); <br/> bitmapsetresolution (bitmap, 72, 72); </P> <p> // create a new bitmap canvas, set the image display quality and text display quality <br/> G = graphicsfromimage (Bitmap); <br/> graphicssetsmoothingmod E (G, smoothingmodeantialias); <br/> graphicssettextrenderinghint (G, textrenderinghintantialias ); </P> <p>/* draw the original image on the canvas */<br/> graphicsdrawimage (G, photo, 0, 0, phwidth, phheight ); </P> <p>/* Original watermark image */</P> <p> // create an image display attribute object <br/> ATTR = imageattrcreate (); <br/> // set the transparent color to the background color of the watermark image, and the watermark image is displayed as a rounded corner image <br/> imageattrsetcolorkey (ATTR, 0xff00ff00, 0xff00ff00, coloradjusttypebitmap ); <br/> // sets the watermark image opacity to 0.3 <br/> imagea Ttrsetcolormatrix (ATTR, & colormatrix, colormatrixflagsdefault, coloradjusttypebitmap); <br/> // draw the original watermark in the upper left corner of the canvas <br/> drect = makerect (10, 10, wmwidth, wmheight); <br/> graphicsdrawimagerectrect (G, watermark, & drect, 0, 0, wmwidth, wmheight, unitpixel, ATTR ); </P> <p>/* center text under the canvas */</P> <p> // create a font object <br/> font = fontcreate (L "Arial ", 16, fontstylebold); <br/> // create a paint brush object <br/> brush = solidbrushcreate (0x9900000 0); <br/> // create a text format object <br/> Format = strformatcreate (); <br/> // set text center display <br/> strformatsetalignment (format, stringalignmentcenter ); <br/> // draw text shadow <br/> frect = makerectf (phwidth/2 + 1, phheight-26 + 1, 0, 0 ); <br/> graphicsdrawstring (G, l "Copyright 2010-maozefa", Font, brush, & frect, format ); <br/> // draw text <br/> frect = makerectf (phwidth/2, phheight-26, 0, 0); <br/> solidbrushsetcolor (brush, 0x 99 ffffff); <br/> graphicsdrawstring (G, l "Copyright 2010-maozefa", Font, brush, & frect, format ); </P> <p> imageattrdelete (ATTR); <br/> strformatdelete (format); <br/> fontdelete (font); <br/> brushdelete (Brush ); <br/> graphicsdelete (g); <br/>}</P> <p> void ondestroy (void) <br/>{< br/> If (hfont) <br/> deleteobject (hfont); <br/> imagedelete (Bitmap); <br/> imagedelete (photo); <br/> imagedelete (watermark ); <br/>}</P> <P> void oncommand (word policycode, word Itemid, hwnd ctlhandle) <br/>{< br/> encoderparameters parameters; <br/> uint Quality = 100; <br/> guid CLSID; </P> <p> If (Itemid! = Id_save) return; <br/> // set the image quality encoding parameters <br/> parameters. count = 1; <br/> parameters. parameter [0]. guid = encoderquality; <br/> parameters. parameter [0]. type = encoderparametervaluetypelong; <br/> parameters. parameter [0]. numberofvalues = 1; <br/> // set the parameter value: quality level. The maximum value is 100. The image file size is proportional to the quality. <br/> parameters. parameter [0]. value = & quality; <br/> // Save the watermark image <br/> If (getencoderclsid (L "image/JPEG", & CLSID )) <br/> imagesavetofile (bitmap, l "d: // watermarkphoto.jpg", & CLSID, paimeters ); <br/>}</P> <p> void onpaint (hdc dc) <br/>{< br/> pgpgraphics G = graphicscreate (DC ); // GDI + canvas <br/> // display the original image <br/> graphicsdrawimage (G, photo, 0, 0, phwidth, phheight ); <br/> // display the original watermark image <br/> graphicstranslate (G, 0, phheight + 5, matrixorderprepend); <br/> graphicsdrawimage (G, watermark, 0, 0, wmwidth, wmheight); <br/> // display watermark and text images <br/> graphicstranslate (G, phwidth,-(phheight + 5), matrixorderprepend ); <br/> graphicsdrawimage (G, bitmap, 0, 0, phwidth, phheight); <br/> graphicsdelete (g ); <br/>}</P> <p> winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) <br/>{< br/> // The Double Buffer painting window is used here */<br/> initapplication (hinstance, ncmdshow, true ); <br/> createproc = oncreate; <br/> destroyproc = ondestroy; <br/> paintproc = onpaint; <br/> commandproc = oncommand; <br/> return runapplication (text ("demo of C language gdiplus-watermark image"), 518,280); <br/>}< br/> // watermark //---------------------------------------------------------------------------

Steps for making a watermark image in this example:

1. Open the original image and the original watermark image;

2. Create a new image, that is, the watermark image to be made, which is the same size as the original image;

3. Draw the original image to the new image;

4. Use the image display attribute object to set the Key Color of the original watermark image, and set the green pixels of the four corners to the transparent color. In this way, the displayed image is in the rounded corner shape;

5. Use the image display attribute object to set the color matrix of the original watermark image so that it is displayed at 30% opacity;

6. display the original watermark image to the upper left corner of the New Image Based on the set image display attribute object;

7. Set the text display format to center text display;

8. The center coordinate of the copyright text display is 1 pixel to the bottom right, and the copyright text shadow is displayed in the new image with 0x99000000 color. The 0x99 color indicates the text shadow color opacity;

9. Use the center coordinate of the copyright text display and 0x99ffffff color to display the copyright text in the new image. 0x99 in the color indicates the text color opacity;

10. production is complete.

This example combines the common image operations and text display processes of GDI +, including the application of image display attribute objects and text display format objects.

The original C ++ version of graphics. the drawstring function has an overload method that uses the X and Y coordinates as parameters. Version C deletes it and only retains the layout rectangle as the parameter, the Code frect = makerectf (phwidth/2, phheight-26, 0, 0) in the example is to create a layout rectangle, where the X and Y parameters are the center point of text display, rather than the coordinates in the upper left corner of the text display, this is because the text format in the example is set to center display (strformatsetalignment (format, stringalignmentcenter);), so the start coordinate point of the text display must be the center point of the text; the width and height parameters are set to 0, which means that the boundary between the right and bottom of the text display is not restricted. This is equivalent to the overload method where the original C ++ version uses the X and Y coordinates as parameters, if the length of the text exceeds the limit, the text will be truncated. Otherwise, if the width and height are given, the text will be wrapped when the length exceeds the limit.

The example also demonstrates how to save the image and provides the code to set the image quality encoding parameters (only valid for JPEG images ). The getencoderclsid function used to save images is not available in the original GDI +. To facilitate the compilation and saving of image code, I wrote this function in the gdipimagecode_c.h header file of the GDI +. If the image encoding parameter is not set, set encoderparams in the imagesavexxxx series functions to null, but the clsidencoder parameter is required.

For the window framework code used by the example code and the GDI + C language version, see "using GDI + in C Windows Applications".

The following is an example of the running interface, where the top left is the original image, the bottom left is the original watermark image, and the right is the watermark image with the copyright stamp:

Please send to: mao.maozefa@gmail.com, maozefa@hotmail.com

 

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.