Add watermarks to uploaded images

Source: Internet
Author: User

 

Make the upload function a page and use IFRAME to directly call the page to be uploaded:

Call Page code:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

The principle is as follows: first load the uploaded image, create a canvas (Bitmap) based on the image as the background, and then create a drawing layer (graphics) on the canvas ), then draw the specified word, color, and position on the layer. after the words are painted, they are regenerated into a new name. Then, they are saved to the image with the new name, and the original image is deleted. and return the path of the latest image to the call page. OK!

Upfile. aspx. csusing system; using system. collections; using system. configuration; using system. data; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. io; public partial class admin_upfile: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protec Ted void button#click (Object sender, eventargs e) {If (request. querystring ["ID"] = NULL | request. querystring ["ID"] = "") {response. write ("<SCRIPT> alert (\" parameter error! \ "); Location. href = 'upfile. aspx '; </SCRIPT>"); response. End () ;}if (fileupload1.postedfile! = NULL) {string filename; // file name string newname; string newpathurl; filename = fileupload1.postedfile. filename; string exten = path. getextension (filename ). tolower (); // The extension if (exten! = ". GIF" & exten! = ". Jpg" & exten! = ". Jpeg" & exten! = ". BMP ") // determine the file type {response. write ("<SCRIPT> alert ('images only support -- GIF | JPG | JPEG | BMP '); location. href = 'upfile. aspx '; </SCRIPT> "); response. end ();} newname = sjname () + ". "+ exten; // new name: String pathurl; pathurl = server. mappath ("~ ") +" \ Uploadfile \ "+ newname; // file storage path string kb = fileupload1.filecontent. length. tostring (); // file size unit: byte fileupload1.postedfile. saveas (pathurl); If (checkbox1.checked) // If a watermark is added {try {using (system. drawing. bitmap Bm = new system. drawing. bitmap (pathurl) // create an image as the background canvas {using (system. drawing. graphics G = system. drawing. graphics. fromimage (BM) // create a layer on the canvas {G. drawstring ("Image Source (www. xiaojiang-de Sign.com) ", new system. drawing. font ("", 15), system. drawing. brushes. yellow, BM. width-350, BM. height-50); // draw the word} newname = sjname () + ". "+ exten; // The new name newpathurl = server. mappath ("~ ") +" \ Uploadfile \ "+ newname; // file storage path BM. save (newpathurl); // Save the image BM with the watermark added. dispose ();} If (file. exists (pathurl) {file. delete (pathurl); // Delete the source image} catch {} response. write ("<SCRIPT> parent.doc ument. form1. "+ request. querystring ["ID"]. tostring () + ". value = '/uploadfile/"+ newname +"'; </SCRIPT> "); // pass the new file path after the upload is successful to the control passed by the parent page }}/// <summary> /// generate a prompt name /// </Summary >/// <returns> </returns> Public String sjname () {string SJ = NULL; SJ = datetime. now. year. tostring () + datetime. now. month. tostring () + datetime. now. day. tostring () + datetime. now. timeofday. hours. tostring () + datetime. now. timeofday. minutes. tostring () + datetime. now. timeofday. milliseconds. tostring (); Return SJ ;}}

 

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.