图片加水印和防盗链(一)

  图片加水印和防盗链(一)

       这几天看了几篇关于IHttpHandler和IHttpModule的文章,对此深有感触,这些东西网上也有很多范例,我写出来的目的也是方做笔记,当然也是一种学习,同时也希望能帮助有需要的人。总的来说这2个东西的作用还是挺大的,在遇到一些问题比较难处理的时候,这2个东西的作用不可忽视。也不多说,直接上作品展示才是有力的武器。

        一、首先建立一个类库

建立类库可以比较好的直接引用进自己的项目当中,在此类库中,创建一个类,ImageHttpHandler.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
///ImageHandler 的摘要说明
/// </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>
        /// 给图片添加水印
        /// </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 font = new Font("宋体", fontSize);
            Brush 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;
        }
    }
}

一一说明,首先判断是否有缓存,如果有的话就直接从缓存中取加水印的图片,没有的话就需要重新制作水印图片。img.Save(context.Response.OutputStream, ImageFormat.Jpeg);是把当前加水印的图片输出到当前响应流中。


上面要保证正确运行的话需要在自己项目中(不是类库,而是引用了当前类库的web项目中引用当前dll文件),配置如下:

<?xml version="1.0"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <appSettings>
        <add key="WaterMark" value="这是我的水印文字"/>
        <add key="FontSize" value="14"/>
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <httpHandlers>
            <add path="*.jpg" type="CustomerHandler2.ImageHandler,CustomerHandler2" verb="*"/>
        </httpHandlers>
    </system.web>
</configuration>

上面有2点需要注意:

1、类库中            string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
                            int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["FontSize"].ToString());

这2段是需要在web项目的web.config中进行配置的

2、        <httpHandlers>
            <add path="*.jpg" type="CustomerHandler2.ImageHandler,CustomerHandler2" verb="*"/>
            </httpHandlers>这个很重要,这个是注册该处理,path指明处理的对象是所有的jpg图片,当然也可以灵活配置(比如 某文件夹/*.jpg的图片等),type第一个是指明是dll+处理类,第二个是指明dll文件名,verb是说明get还是post等,*指明是所有的请求

 

最后在自己的web项目中随便引用一张图片就可以正确生成加水印图片了(此水印图片在实际文件夹中并没有加水印,只是在显示的时候添加了水印)

原文地址:https://www.cnblogs.com/javawebsoa/p/2987571.html