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