/// <Summary>
/// Creating a watermarked photograph with gdi + for. net
/// </Summary>
/// <Param name = "rsrcimgpath"> physical path of the original image </param>
/// <Param name = "rmarkimgpath"> physical path of the Watermark Image </param>
/// <Param name = "rmarktext"> watermark text (do not show the watermark text as a blank string) </param>
/// <Param name = "rdstimgpath"> output the physical path of the merged image. </param>
// @ Finishing: anyrock@mending.cn
Public void buildwatermark (string rsrcimgpath, string rmarkimgpath, string rmarktext, string rdstimgpath)
{
// The following code creates an image object from a specified file and defines variables for its width and height.
// These lengths will be used to create a bitmap object that uses the format of 24 bits per pixel as the color data.
Image imgphoto = image. fromfile (rsrcimgpath );
Int phwidth = imgphoto. width;
Int phheight = imgphoto. height;
Bitmap BMP hoto = new bitmap (phwidth, phheight, pixelformat. format24bpprgb );
BMP hoto. setresolution );
Graphics grphoto = graphics. fromimage (BMP hoto );
// This code loads the watermark image. The watermark image is saved as a bmp file in green (a = 0, r = 0, g = 255, B = 0) as the background color.
// Once again, a variable is defined for its width and height.
Image imgwatermark = new bitmap (rmarkimgpath );
Int wmwidth = imgwatermark. width;
Int wmheight = imgwatermark. height;
// This code draws imgphoto to the position of the graphics object (x = 0, y = 0) at the original size of 100%.
// All future drawings will occur on the top of the original photo.
Grphoto. smoothingmode = smoothingmode. antialias;
Grphoto. drawimage (
Imgphoto,
New rectangle (0, 0, phwidth, phheight ),
0,
0,
Phwidth,
Phheight,
Graphicsunit. pixel );
// To maximize the copyright information size, we will test 7 different font sizes to determine the maximum size we can use for our photo width.
// To effectively complete this operation, we define an integer array and traverse these integer values to measure copyright strings of different sizes.
// Once we determine the maximum possible size, we exit the loop and draw the text
Int [] sizes = new int [] {16, 14, 12, 10, 8, 6, 4 };
Font crfont = null;
Sizef crsize = new sizef ();
For (int I = 0; I <7; I ++)
{
Crfont = new font ("arial", sizes [I],
Fontstyle. bold );
Crsize = grphoto. measurestring (rmarktext,
Crfont );
If (ushort) crsize. width <(ushort) phwidth)
Break;
}