This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14691|Reply: 0

[ASP.NET] Use the general handler (IHttpHandler) to make a global watermark of the image

[Copy link]
Posted on 9/10/2015 11:37:26 PM | | | |


Watermarks are meant to prevent us from stealing our images.

Two ways to achieve the watermark effect

1) Watermark can be added when the user uploads.

a) Benefits: Compared to the 2 methods, the server sends this image directly to the customer every time the user reads it.

b) Cons: Destroys the original picture.

2) Through the global general processing program, when the user requests this image, add a watermark.

a) Benefits: The original picture is not destroyed

b) Disadvantages: Users need to watermark the requested images every time they make a request, which wastes the server's resources.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Drawing;
  6. using System.IO;

  7. namespace Watermark
  8. {

  9.     /// <summary>
  10.     /// waterH 的摘要说明
  11.     /// </summary>
  12.     public class waterH : IHttpHandler
  13.     {
  14.         public waterH()
  15.         { }
  16.         private const string WATERMARK_URL = "~/Test/watermark.png";
  17.         private const string DEFAULTIMAGE_URL = "~/Test/watermark.png";
  18.         public void ProcessRequest(HttpContext context)
  19.         {
  20.             System.Drawing.Image Cover;
  21.             if (File.Exists(context.Request.PhysicalPath))
  22.             {
  23.                 Cover = Image.FromFile(context.Request.PhysicalPath);
  24.                 Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));
  25.                 Graphics g = Graphics.FromImage(Cover);
  26.                 g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
  27.                 g.Dispose();
  28.                 watermark.Dispose();
  29.             }
  30.             else
  31.             {
  32.                 Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));
  33.             }
  34.             context.Response.ContentType = "images/jpeg";
  35.             Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  36.             Cover.Dispose();
  37.             context.Response.End();
  38.         }

  39.         public bool IsReusable
  40.         {
  41.             get
  42.             {
  43.                 return false;
  44.             }
  45.         }
  46.     }
  47. }
Copy code
The file code for waterH.ashx is as follows:

Then you need to configure the following node on the web.config:

Under the system.web node
    <httpHandlers>
      <add verb="*" path="Images/*.jpg" type="Watermark.waterH"/>
    </httpHandlers>

At the beginning, the nodes I configured were:
    <httpHandlers>
      <add verb="*" path="Images/*.jpg" type="waterH"/>
    </httpHandlers>

will report an error,
Misconfiguration

Description: An error occurred when processing the profile required to provide services to the request. Please check the specific error details below and modify the configuration file appropriately.

Analyzer error message: Failed to load type "waterH".

Source error:


Line 15: <system.web>
Line 16<httpHandlers>:
Line 17: <add verb="*" path="Images/*.jpg" type="waterH"/>
Line 18: </httpHandlers>
Line 19: <compilation debug="true" targetFramework="4.0" />




Finally, inwaterH with the name of the project!
Finally, attach a copy of the source code: Watermark.rar (234.64 KB, Number of downloads: 0, 售价: 2 粒MB)




Previous:360 Coolpad Zhou Hongyi and Jiang Chao tear each other apart or hype?
Next:The website js hangs on the horse idea, and the external transfer is j/c
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com