Watermarks and anti-leech protection for images (1)

Source: Internet
Author: User
Watermarks and anti-leech protection for images (1)

I have read several articles about ihttphandler and ihttpmodule over the past few days.ArticleI am deeply touched by this. There are also many examples of these things on the Internet. The purpose I have written is to take notes, and of course it is also a kind of learning. I also hope to help people in need. In general, these two things play a very major role. When some problems are difficult to solve, the role of these two things cannot be ignored. Not to mention, simply displaying works is a powerful weapon.

1. First, create a class library

You can directly reference a class library to your project. In this class library, create a class, imagehttphandler. CS,CodeAs follows:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. drawing; using system. drawing. imaging; using system. io; // <summary> /// imagehandler abstract description /// </Summary> namespace customerhandler2 {public class imagehandler: ihttphandler {public void processrequest (httpcontext context) {string imgurl = context. request. physicalpath; If (file. exists (imgurl) {bitmap IMG; If (context. cache [imgurl] = NULL) {IMG = new Bitmap (imgurl); IMG = addwatermark (IMG); context. cache [imgurl] = IMG;} else IMG = context. cache [imgurl] As bitmap; IMG. save (context. response. outputstream, imageformat. JPEG) ;}} public bool isreusable {get {return true ;}} /// <summary> /// Add a watermark to the image /// </Summary> /// <Param name = "image"> </param> /// <returns> </returns> private bitmap addwatermark (bitmap image) {string text = system. configuration. configurationmanager. appsettings ["watermark"]. tostring (); int fontsize = int. parse (system. configuration. configurationmanager. appsettings ["fontsize"]. tostring (); font = new font ("", fontsize); brush = brushes. red; graphics G = graphics. fromimage (image); sizef size = G. measurestring (text, font); G. drawstring (text, Font, brush, image. width-size. width, image. height-size. height); G. dispose (); Return image ;}}}

One by one, we first determine whether there is a cache. If so, we can directly extract the watermark image from the cache. If not, we need to recreate the watermark image. IMG. Save (context. response. outputstream, imageformat. JPEG); outputs the image with the current watermark to the current response stream.

To ensure proper running, you must reference the current DLL file in your own project (instead of a class library). The configuration is as follows:

 
<? XML version = "1.0"?> <! -- About how to configure ASP. NET ApplicationsProgramFor more information, visit the http://go.microsoft.com/fwlink? Linkid = 169433 --> <configuration> <etettings> <add key = "watermark" value = "this is my watermark text"/> <add key = "fontsize" value = "14 "/> </appsettings> <system. web> <compilation DEBUG = "true" targetframework = "4.0"/>  

Note the following two points:

1. In the class library, string text = system. configuration. configurationmanager. etettings ["watermark"]. tostring ();
Int fontsize = int. parse (system. configuration. configurationmanager. deleettings ["fontsize"]. tostring ());

These two sections need to be configured in the web. config of the web Project.

2. <Add Path = "*. jpg" type = "customerhandler2.imagehandler, customerhandler2" verb = "*"/>
</Httphandlers> This is very important. It indicates that the processed object is all JPG images. Of course, it can be flexibly configured (such as a folder /*. JPG images). The first type indicates the dll + processing class, the second is the DLL file name, the verb indicates get or post, and * indicates all requests.

 

Finally, you can reference an image in your web project to generate a watermark image correctly. (The watermark image is not added to the actual folder, but is added to the display)

 

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.