(C #) adds a watermark to the image

Source: Internet
Author: User

In the following code, the code for adding a text watermark and an image watermark cannot coexist. It is written in one piece for ease of display.
Private void Btn_Upload_Click (object sender, System. EventArgs e)
If (UploadFile. PostedFile. FileName. Trim ()! = "")
// Upload a file
String extension = Path. GetExtension (UploadFile. PostedFile. FileName). ToUpper ();
String fileName = DateTime. Now. ToString ("yyyyMMddhhmmss ");
String path = Server. MapPath (".") + "/UploadFile/" + fileName + extension;
UploadFile. PostedFile. SaveAs (path );
// Add a text watermark. Note that the code here cannot coexist with the image watermark code below.
System. Drawing. Image image = System. Drawing. Image. FromFile (path );
Graphics g = Graphics. FromImage (image );
G. DrawImage (image, 0, 0, image. Width, image. Height );
Font f = new Font ("Verdana", 32 );
Brush B = new SolidBrush (Color. White );
String addText = AddText. Value. Trim ();
G. DrawString (addText, f, B, 10, 10 );
G. Dispose ();
// Add an image watermark
System. Drawing. Image image = System. Drawing. Image. FromFile (path );
System. Drawing. Image copyImage = System. Drawing. Image. FromFile (Server. MapPath (".") + "/Alex.gif ");
Graphics g = Graphics. FromImage (image );
G. drawImage (copyImage, new Rectangle (image. width-copyImage.Width, image. height-copyImage.Height, copyImage. width, copyImage. height), 0, 0, copyImage. width, copyImage. height, GraphicsUnit. pixel );
G. Dispose ();
// Save the watermark image and delete the original image
String newPath = Server. MapPath (".") + "/UploadFile/" + fileName + "_ new" + extension;
Image. Save (newPath );
Image. Dispose ();
If (File. Exists (path ))
File. Delete (path );
Response. Redirect (newPath );

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: 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.