Use GDI + to create watermark image for Delphi Program

Source: Internet
Author: User
Tags transparent color

Using GDI +, you can easily create images with watermark effects.ArticleThere are also many, but there are few Delphi, this article refer to the online article http://www.codeproject.com/kb/gdi-plus/watermark.aspxintroduction, using Delphi 2007 to create a watermark, the originalCodeAs follows:

Unit Main;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, gdiplus, stdctrls;

Type
Tmainform = Class (tform)
Button1: tbutton;
Procedure formpaint (Sender: tobject );
Procedure formcreate (Sender: tobject );
Procedure formdestroy (Sender: tobject );
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Phot tgpimage;
Phwidth: integer;
Phheight: integer;
Watermark: tgpimage;
Wmwidth: integer;
Wmheight: integer;
BMP: tgpbitmap;
Public
{Public declarations}
End;

VaR
Mainform: tmainform;

Implementation

Uses gdiptypes;

{$ R *. DFM}

Procedure tmainform. button1click (Sender: tobject );
VaR
CLSID: tguid;
Parameters: tencoderparameters;
Quality: integer;
Begin
// Set image quality encoding Parameters
Parameters. Count: = 1;
Parameters. Parameter [0]. guid: = encoderquality;
Parameters. Parameter [0]. valuetype: = encoderparametervaluetypelong;
Parameters. Parameter [0]. numberofvalues: = 1;
// Set the parameter value: quality level, up to 100. The image file size is proportional to the quality.
Quality: = 100;
Parameters. Parameter [0]. Value: = @ quality;

If getencoderclsid ('image/JPEG ', CLSID) then
BMP .save('watermarkphoto.jpg ', CLSID, @ parameters );
End;

Procedure tmainform. formcreate (Sender: tobject );
Const
Colormatrix: tcolormatrix =
(
(1.0, 0.0, 0.0, 0.0, 0.0 ),
(0.0, 1.0, 0.0, 0.0, 0.0 ),
(0.0, 0.0, 1.0, 0.0, 0.0 ),
(0.0, 0.0, 0.0, 0.3, 0.0 ),
(0.0, 0.0, 0.0, 0.0, 1.0)
);
Workingdirectory = '... media ';
Copyright = 'copyright 2008-maozefa ';
VaR
GP: tgpgraphics;
Imageattr: tgpimageattributes;
Strformat: tgpstringformat;
Font: tgpfont;
X, Y: single;
Begin
// Read the original image
Photo: = tgpimage. Create (workingdirectory + '100_0349.jpg ');
Phwidth: = photo. width;
Phheight: = photo. height;
// Read the watermark image
Watermark: = tgpimage. Create (workingdirectory + 'watermark.bmp ');
Wmwidth: = watermark. width;
Wmheight: = watermark. height;
// Create a new bitmap with a resolution of 72
BMP: = tgpbitmap. Create (phwidth, phheight, pf32bppargb );
BMP. setresolution (72, 72 );
// Create a new bitmap canvas and set the image display quality and text display quality.
GP: = tgpgraphics. Create (BMP );
GP. smoothingmode: = smantialias;
GP. textrenderinghint: = thantialias;
// Draw the original image on the canvas
GP. drawimage (photo, gprect (0, 0, phwidth, phheight ),
0, 0, phwidth, phheight, utpixel );
// Create an image display helper class
Imageattr: = tgpimageattributes. Create;
// Set the transparent color to the base color of the watermark image at four corners. The watermark image is displayed as a rounded corner image.
Imageattr. setcolorkey ($ ff00ff00, $ ff00ff00, ctbitmap );
// Set the watermark image opacity to 0.3
Imageattr. setcolormatrix (colormatrix, cfdefault, ctbitmap );
// Watermark in the upper left corner of the canvas
GP. drawimage (watermark, gprect ({phwidth-wmwidth-} 10, 10, wmwidth, wmheight ),
0, 0, wmwidth, wmheight, utpixel, imageattr );
// Set the text font and display format
Font: = tgpfont. Create ('arial', 16, [fsbold]);
Strformat: = tgpstringformat. Create;
Strformat. Alignment: = sacenter;
// Center the shadow text under the canvas
X: = phwidth/2;
Y: = phheight-26;
GP. drawstring (copyright, Font, brushs [$99000000], x + 1, Y + 1, strformat );
GP. drawstring (copyright, Font, brushs [$99 ffffff], X, Y, strformat );

Font. Free;
Strformat. Free;
Imageattr. Free;
GP. Free;
End;

Procedure tmainform. formdestroy (Sender: tobject );
Begin
Photo. Free;
Watermark. Free;
BMP. Free;
End;

Procedure tmainform. formpaint (Sender: tobject );
VaR
G: tgpgraphics;
Begin
G: = tgpgraphics. Create (canvas. Handle );
// Display the original image
G. drawimage (photo, 0, 0, phwidth, phheight );
// Display the original watermark image
G. translatetransform (0, phheight + 5 );
G. drawimage (watermark, 0, 0, wmwidth, wmheight );
// Display images with watermarks and text
G. translatetransform (phwidth,-(phheight + 5 ));
G. drawimage (BMP, 0, 0, phwidth, phheight );
G. Free;
End;

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.