水印Handler方式

本文介绍了两种在ASP.NET中添加水印的方法:指定Handler方式和全局Handler方式。通过编写处理程序,实现对图片添加文字水印,如'https://blog.youkuaiyun.com/qq_41865271博客',并展示了如何在web.config中配置以全局应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、指定Handler方式

1、添加Handler一般处理程序


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(“搜索”"https://blog.youkuaiyun.com/qq_41865271博客",“王文龙”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=数字就可以了,这时我们就可以看到封面图片的右下角添加上"https://blog.youkuaiyun.com/qq_41865271编程博客"的标识,完成了数字水印的效果。接着我们打开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. <https://blog.youkuaiyun.com/qq_41865271">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.    
  14.     </form>  
  15. </body>  
  16. </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("https://blog.youkuaiyun.com/qq_41865271", 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、就到这把 ! 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值