Use a general processing program (ihttphandler) to create an image watermark

Source: Internet
Author: User

When creating a website, you often need to add a watermark of the website name to the image. In this way, the source of the image may appear when others repost the image, which is conducive to website promotion. However, if you use PS to add watermarks one by one, the workload will be huge, and after modification, you will not be able to restore them. This tutorial teaches you how to use a general processing program (handler) to create an image watermark. The advantage of this watermark is that it applies to the entire site once it is modified, and does not change the original image.

You can use a local handler to add a watermark to an image in a specified path, or a global handler to make a watermark. The former allows you to add a watermark to a specified image flexibly, but it is inconvenient to add a handler address to each link, which is not conducive to overall modification. The latter adds watermarks to all images in the specified folder.

I. Local handler:

1. Add the General handler watermarkhandler. ashx.

<% @ Webhandler Language = "C #" class = "watermarkhandler" %> using system; using system. web; using system. io; using system. drawing; public class watermarkhandler: ihttphandler {string waterpath = "~ /Images/1/watermark.png "; // watermark image path string defaultpath = "~ /Images/1/default.jpg "; // default image path public void processrequest (httpcontext context) {string coverpath = context. server. mappath (context. request. params ["path"]); image cover; If (file. exists (coverpath) {// load image cover = image. fromfile (coverpath); // load the watermark image water = image. fromfile (context. request. mappath (waterpath); // instantiate the canvas graphics G = graphics. fromimage (cover); // draw an image G. drawimage (water, new rectangle (cover. width-water. width, cover. height-water. height, water. width, water. height), 0, 0, water. width, water. height, graphicsunit. pixel); // release the canvas G. dispose (); // release the watermark water. dispose ();} else // no image. The default image {cover = image is displayed. fromfile (context. request. mappath (defaultpath);} context. response. contenttype = "image/JPEG"; cover. save (context. response. outputstream, system. drawing. imaging. imageformat. JPEG); cover. dispose (); context. response. end () ;}public bool isreusable {get {return false ;}}}

2. Call httphandler on the aspx page:

 

In this way, the image can be output when the image control accesses handler.

Ii. Global handler:

This method does not need to create httphandler. ashx, you only need to create a class in the app_code folder, and then specify this class in the configuration file. The Page Accessing the image on aspx does not need to be changed:

1. Configure web. config:

    <system.web>        <compilation debug="true" targetFramework="4.0" />        

2. Global handler watermarkhandler code:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. drawing; using system. io; // <summary> // Summary of coverhandler // 1. configure the configuration file to capture image access requests // 2. create a class file coverhandler. CS // 3. method for implementing the ihttphandler interface /// </Summary> public class watermarkhandler: ihttphandler {public watermarkhandler () {// todo: add the constructor logic here // 4. the setting cannot allow reuse of public bool isreusable {get {return false ;}} // 5. write the final processing program public void processrequest (httpcontext context) {// 1. Obtain the cover image and watermark image. The default image path is string coverpath = context. request. physicalpath; string waterpath = context. server. mappath ("~ /Images/1/default.jpg "); // watermark image string defaultpath = context. server. mappath ("~ /Images/1/default.jpg "); // default image // 2. draw an image cover; // define the cover image object if (file. exists (coverpath) // ** Note: Determine whether the file exists based on the physical path ** {// 2.1 load the cover image cover = image. fromfile (coverpath); // 2.2 load the watermark image water = image. fromfile (waterpath); // 2.3 instantiate the canvas ** the covered image is used as the canvas ** graphics G = graphics. fromimage (cover); // 2.4 draw the cover image G. drawimage (water, // watermark image new rectangle (cover. width-water. width, cover. height-water. height, water. width, water. height), 0, // horizontal offset 0, // vertical offset water. width, // draw the width of water. height, // draw the graphicsunit height. pixel); // paint brush size // 2.5 release canvas, watermark G. dispose (); water. dispose ();} else // if the image does not exist, use the default image {// 2.6 to set the default page as cover image cover = image. fromfile (defaultpath);} // 3 sets the output content type context. response. contenttype = "image/JPEG"; // 4 Save the modified image to the file stream. save (context. response. outputstream, // output stream of the file to be saved to system. drawing. imaging. imageformat. JPEG); // save the file type // 5. release the image cover. dispose (); context. response. end () ;}/// Note: // 1. configure the configuration file to capture image access requests // 2. coverhandler. CS needs to create a class on the website and save it in the app_code folder as prompted. The "General handler" cannot be used; // 3. method for implementing the ihttphandler interface; // 4. the setting cannot be reused. // 5. write the final processing program

3. No processing is required on the ASPX page. when the request is image/*. jpg, it is processed in the handler mybookmark and the watermark image is also output.

Use a general processing program (ihttphandler) to create an image watermark

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.