一、指定Handler方式

1、添加Handler一般处理程序

ASP.NET Httphandler添加水印

2、PicHandler.ashx源码如下:

需要的引用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Drawing;
using System.IO;

[csharp] view plain copy
  1. public class PicHandler: IHttpHandler  
  2.    {  
  3.   
  4.        //图片路径  
  5.        string IMG = "~/ProductImgs/";  
  6.        //默认图片路径  
  7.        string DefaultImg = "~/ProductImgs/default.jpg";  
  8.        public void ProcessRequest(HttpContext context)  
  9.        {  
  10.            //获取要添加图片的路径  
  11.            string path = context.Request.MapPath(IMG + context.Request.QueryString["id"].ToString() + ".jpg");  
  12.            Image image;  
  13.            //判断图片是否存在  
  14.            if (File.Exists(path))  
  15.            {  
  16.                //加载图片文件  
  17.                image = Image.FromFile(path);  
  18.                //定义画布  
  19.                Graphics graphics = Graphics.FromImage(image);  
  20.                //加水印  
  21.                graphics.DrawString("编程博客"new Font("微软雅黑", 12), Brushes.Red, image.Width - 125, image.Height - 15);  
  22.                //释放画布  
  23.                graphics.Dispose();  
  24.   
  25.            }  
  26.            else  
  27.            {  
  28.                //如果图片不存在的话则显示默认图片  
  29.                image = Image.FromFile(DefaultImg);  
  30.            }  
  31.            //设置输出的图片格式  
  32.            context.Response.ContentType = "image/jepg";  
  33.            //将修改的图片存入输出流  
  34.            image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
  35.            //释放图片  
  36.            image.Dispose();  
  37.            //终止输出  
  38.            context.Response.End();  
  39.        }  
  40.   
  41.        public bool IsReusable  
  42.        {  
  43.            get  
  44.            {  
  45.                return false;  
  46.            }  
  47.        }  
  48.    }  

3、修改图片路径

我们还要做的就是,将所有需要使用数字水印访问图片的路径修改为"PicHandler.ashx?id=数字就可以了,这时我们就可以看到封面图片的右下角添加上"编程博客"的标识,完成了数字水印的效果。接着我们打开ProductImgs文件夹查看封面图片的原图,发现原图没有做任何的修改。真是太神奇了!

[html] view plain copy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pic.aspx.cs" Inherits="ASP.NET水印._Default" %>  
  2.   
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.   
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.         <asp:Image ID="Image1" runat="server" ImageUrl="~/ProductImgs/1.jpg" />  
  15.         <asp:Image ID="Image2" runat="server" ImageUrl="~/ProductImgs/2.jpg" />  
  16.         <asp:Image ID="Image3" runat="server" ImageUrl="~/ProductImgs/3.jpg" />  
  17.         <asp:Image ID="Image4" runat="server" ImageUrl="~/ProductImgs/4.jpg" /><br />  
  18.         <asp:Image ID="Image5" runat="server" ImageUrl="~/PicHandler.ashx?id=1" />  
  19.         <asp:Image ID="Image6" runat="server" ImageUrl="~/PicHandler.ashx?id=2" />  
  20.         <asp:Image ID="Image7" runat="server" ImageUrl="~/PicHandler.ashx?id=3" />  
  21.         <asp:Image ID="Image8" runat="server" ImageUrl="~/PicHandler.ashx?id=4" />  
  22.     </div>  
  23.     </form>  
  24. </body>  
  25. </html>  


二、全局Handler方式

1、修改web.config,将所有对.jpg内容的访问转到Httphandler处理程序。

[html] view plain copy
  1. <httpHandlers>  
  2.  <add verb="*" path="~/ProductImgs/*.jpg" validate="false" type="PicCoverHandler"/>  
  3.     </httpHandlers>  

2、PicCoverHandler源码

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

3、最后一步,运行到浏览器查看就可以啦。


相关文章:

  • 2021-12-12
  • 2021-12-29
  • 2021-10-16
  • 2022-02-08
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-03
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案