Default. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd&201734;>
<HTML xmlns = "http://www.w3.org/5o/xhtml&%34;>
<Head runat = "server">
<Title> upload... </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "file1" type = "file" size = "47" name = "file1" runat = "server"> <br/>
<Asp: button id = "button1" runat = "server" text = "upload and add a text watermark" onclick = "button#click1"> </ASP: button>
<Asp: button id = "button2" runat = "server" text = "upload and add an image watermark" onclick = "button2_click1"> </ASP: button>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server" errormessage = "*" controltovalidate = "file1"> </ASP: requiredfieldvalidator>
<Br/>
<Asp: Image id = "image1" runat = "server" imagealign = "Middle"> </ASP: Image>
</Div>
</Form>
</Body>
</Html>
Default. aspx. CS
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
}
Protected void button1_click1 (Object sender, eventargs E)
{
If (file1.postedfile. filename. Trim ()! = "")
{
// Upload a file
String extension = path. getextension (file1.postedfile. filename). tolower ();
String filename = datetime. Now. tostring ("yyyymmddhhmmss ");
String Path = server. mappath (".") + "/upload/" + filename + extension;
File1.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", 16 );
Brush B = new solidbrush (color. Blue );
String addtext = "China 114 Yellow Pages ";
G. drawstring (addtext, F, B, 10, 10 );
G. Dispose ();
// Save the watermark image and delete the original image
String newpath = server. mappath (".") + "/upload/" + filename + "_ new" + extension;
Image. Save (newpath );
Image. Dispose ();
If (file. exists (PATH ))
{
File. Delete (PATH );
}
Image1.imageurl = newpath;
}
}
Protected void button2_click1 (Object sender, eventargs E)
{
// Upload a file
String extension = path. getextension (file1.postedfile. filename). toupper ();
String filename = datetime. Now. tostring ("yyyymmddhhmmss ");
String Path = server. mappath (".") + "/upload/" + filename + extension;
File1.postedfile. saveas (PATH );
// Add an image watermark
System. Drawing. Image image = system. Drawing. image. fromfile (PATH );
System. Drawing. Image copyimage = system. Drawing. image. fromfile (server. mappath (".") + "/tm.jpg ");
Graphics G = graphics. fromimage (image );
G. drawimage (copyimage, new rectangle (image. Width-copyimage. width)/2, (image. Height-copyimage. Height)/2,
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 (".") + "/upload/" + filename + "_ new" + extension;
Image. Save (newpath );
Image. Dispose ();
If (file. exists (PATH ))
{
File. Delete (PATH );
}
Image1.imageurl = newpath;
}
}