Dark Horse programmer _ ASP. NET attaches watermarks to images

Source: Internet
Author: User

------- Windows Phone 7 Mobile Phone development,. Net training, and we look forward to communicating with you! -------

 

Attach a specified watermark to all the images in the specified directory as learned in the course;

You need to create a class and inherit: ihttphandler

The Code is as follows:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. drawing; using system. drawing. imaging; using system. io; // <summary> /// Summary of coverhandler // </Summary> public class coverhandler: ihttphandler {// all the URLs of the image to be added with the watermark public const string oldimageurl = "~ /Admin/bookconvers/"; // watermark image public const string waterurl = "~ /Admin/images/watermark.jpg "; // default image public const string defaultimage = "~ /Admin/images/default.jpg "; Public coverhandler () {// todo: add the constructor logic here //} public void processrequest (httpcontext context) {// string Path = context. request. mappath (oldimageurl + context. request. params ["ISBN"] + ". jpg "); system. drawing. image cover; // determines whether the file if (file. exists (context. request. physicalpath) {// load the file // cover = image. fromfile (context. request. physicalpath); cover = image. fromfile (context. request. physicalpath); // load the watermark image watermark = image. fromfile (context. request. mappath (waterurl); // instantiate the canvas graphics G = graphics. fromimage (cover); // draw the watermark G on the cover. drawimage (watermark, new point (cover. width-watermark. width, cover. height-watermark. height); // release the canvas G. dispose (); // release the watermark image watermark. dispose (); // Save the output stream cover. save (context. response. outputstream, system. drawing. imaging. imageformat. JPEG); // end the output cover. dispose (); context. response. end ();} else {cover = image. fromfile (context. request. mappath (defaultimage) ;}} public bool isreusable {get {return false ;}}}

Configuration file:

Because the image size affects the watermark position in the image, try to use images of the same size.

Watermark:

 

------- Windows Phone 7 Mobile Phone development,. Net training, and we look forward to communicating with you! -------

 

 

 

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.