HttpHandle数字水印

    动态地添加数字水印效果,要用到HttpHandle技术。

    ASP.NET请求处理过程是基于管道模型的,这个管道模型由多个HttpModule和HttpHandler组成,ASP.NET把HTTP请求依次传递给管道中的各个HttpModule,最终被HttpHandler处理,处理完成后,再次经过管道中的HTTP模块,把结果返回给客户端。我们可以在每个HttpModule中干预请求的处理过程。

  • 事实上我们每请求一个aspx页面都经历了同样的过程,只不过我们没有察觉而已。
  • 一个请求可以经过多个Module但是最终只能被一个Handler处理。

HttpHandler的使用

在“添加新项”中添加“一般处理程序”,如图所示。


单击“添加”按钮后,创建一个以.aspx为后缀名的文件。默认创建的内容为:

[csharp] view plain copy
  1.   
[csharp] view plain copy
  1. public class Handler1 : IHttpHandler //实现IHttpHandler接口  
  2.   
  3.    {  
  4.   
  5.        public void ProcessRequest(HttpContext context)  
  6.        {//实现ProcessRequest方法  
  7.            context.Response.ContentType = "text/plain";  
  8.            context.Response.Write("Hello World");  
  9.        }  
  10.   
  11.        public bool IsReusable //实现IsReusable属性  
  12.        {  
  13.            get  
  14.            {  
  15.                return false;  
  16.            }  
  17.        }  
  18.    }  

数字水印的实现

  •       直接编辑每张图片(需要大量的人力)
  •       编辑实现批量编辑图片(节省人力,效果高,但原始图片丢失)
  •        在显示图片时,动态添加数字水印效果  (使用HttpHandler)

     1、使用Handler方式实现数字水印

[csharp] view plain copy
  1. public class PicHandler : IHttpHandler  
  2.     {  
  3.   
  4.         //普通图片的虚拟路径  
  5.         private const string IMG = "~/productimgs/";  
  6.         //默认图片的虚拟路径  
  7.         private string defaultimg = "~/productimgs/default.jpg";  
  8.   
  9.         //处理方法  
  10.         public void ProcessRequest(HttpContext context)  
  11.         {  
  12.             //定义新的图片  
  13.             Image Cover;  
  14.             //获取图片物理路径  
  15.             string path = context.Request.MapPath(IMG + context.Request.Params["id"].ToString() + ".jpg");  
  16.             //判断图片的物理路径是否存在  
  17.             if (File.Exists(path))  
  18.             {  
  19.                 //加载图片  
  20.                 Cover = Image.FromFile(path);  
  21.                 //定义字体  
  22.                 Font font = new Font("微软雅黑", 20);  
  23.                 //定义画布  
  24.                 Graphics g = Graphics.FromImage(Cover);  
  25.                 //合成水印图片  
  26.                 g.DrawString("xiecan.cc", font, Brushes.Red, Cover.Width - 90, Cover.Height - 30);  
  27.                 //释放画布  
  28.                 g.Dispose();  
  29.             }  
  30.             else  
  31.             {  
  32.                 //加载默认图片  
  33.                 Cover = Image.FromFile(context.Request.MapPath(defaultimg));  
  34.             }  
  35.             //定义输出类型  
  36.             context.Response.ContentType = "image/jpeg";  
  37.             //保存图片到输出流  
  38.             Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
  39.             //释放画布  
  40.             Cover.Dispose();  
  41.             //输出结束  
  42.             context.Response.End();  
  43.   
  44.         }  
  45.   
  46.         //是否重用  
  47.         public bool IsReusable  
  48.         {  
  49.             get  
  50.             {  
  51.                 return false;  
  52.             }  
  53.         }  
  54.     }  

    2、全局Handler方式实现数字水印

        使用.aspx方式,需要修改所有访问封面图片的路径,太麻烦,能否在不修改任何访问路径的情况下实现图片的数字水印效果?

        实现思路分析

  •      修改配置信息,将所有对.JPG内容的访问转到HttpHandler处理程序
  •       获得访问请求,得到用户访问的图片路径;
  •       根据请求的路径,查找相对应的DVD封面图片;
  •       将网站 标识“绘制”在DVD封面图片的左下角;
  •       修改程序的输出类型,并将组合出的新图片输出。

    配置全局Handler

[csharp] view plain copy
  1. <httpHandlers>  
  2.       <remove verb="*" path="*.asmx"/>  
  3.       <add verb="*" path="ProductImgs/*.jpg" type="第2章_用户控件与HttpHandler.PicCoverHandler"/>  
  4.       <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  5.       <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>  
  6.       <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>  
  7.     </httpHandlers>  
  •    verb:谓词GET、POST、FTP等。
  •    path:访问路径,此处表示所有针对“Productlmgs/*.jpg”路径的请求都将交给PicCoverHandler类进行处理。
  •     type:指定的处理程序(类)。

    URL路径: path="ProductImgs/*.jpg"

    指定类名,新建的类:type="第2章_用户控件与HttpHandler.PicCoverHandler"

    实现数字水印

[csharp] view plain copy
  1. public class PicCoverHandler : IHttpHandler  
  2.    {  
  3.        //默认图片  
  4.        private string defaultimg = "~/productimgs/default.jpg";  
  5.   
  6.   
  7.        public bool IsReusable  
  8.        {  
  9.            get { throw new NotImplementedException(); }  
  10.        }  
  11.   
  12.        public void ProcessRequest(HttpContext context)  
  13.        {  
  14.            //实例化图片  
  15.            Image Cover;  
  16.            //判断图片物理路径是否存在  
  17.            if (File.Exists(context.Request.PhysicalPath))  
  18.            {  
  19.                //加载图片  
  20.                Cover = Image.FromFile(context.Request.PhysicalPath);  
  21.                //定义字体  
  22.                Font font = new Font("微软雅黑", 20);  
  23.                //定义画布  
  24.                Graphics g = Graphics.FromImage(Cover);  
  25.                //合成水印图片  
  26.                g.DrawString("xiecan.cc", font, Brushes.Black, Cover.Width - 90, Cover.Height - 30);  
  27.                //释放画布  
  28.                g.Dispose();  
  29.            }  
  30.            else  
  31.            {  
  32.                Cover = Image.FromFile(context.Request.MapPath(defaultimg));  
  33.            }  
  34.            //定义输出类型  
  35.            context.Response.ContentType = "image/jpeg";  
  36.            //保存图片到输出流  
  37.            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
  38.            //释放图片  
  39.            Cover.Dispose();  
  40.            //终止输出  
  41.            context.Response.End();  
  42.   
  43.        }  
  44.    } 


猜你喜欢

转载自blog.csdn.net/qq_41860950/article/details/80182782