Asp.net C # watermark code (1/3) for images with transparency settings)

Source: Internet
Author: User

/// <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;
     }   
   

Homepage 1 2 3 Last page
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.