<Form id = "form1" runat = "server">
<Div>
<Asp Tutorial: fileupload id = "uploadfile" runat = "server"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
</Div>
</Form>
Default. aspx. cs
Using system;
Using system. configuration;
Using system. data;
Using system. linq;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system.web.ui.html controls;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system. xml. linq;
Using system. drawing;
Using system. io;
Public partial class _ default: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{
}
Protected void button#click (object sender, eventargs e)
{
If (uploadfile. postedfile. filename. trim ()! = "")
{
// Upload a file
String extension = path. getextension (uploadfile. postedfile. filename). toupper ();
String filename = datetime. now. year. tostring () + datetime. now. month. tostring () + datetime. now. day. tostring () + datetime. now. hour. tostring () + datetime. now. minute. tostring () + datetime. now. second. tostring ();
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 = 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 = system. drawing. image. fromfile (path );
System. drawing. image copyimage = system. drawing. image. fromfile (server. mappath (".") + "/tp.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 );
Response. redirect ("default. aspx ");
}
}
}