ASP.NET HttpHandler加水印

一、指定Handler方式

1、添加Handler一般处理程序

2、PicHandler.ashx源码如下:

需要的引用:

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

using System.Drawing;
using System.IO;

 public class PicHandler: IHttpHandler
    {

        //图片路径
        string IMG = "~/ProductImgs/";
        //默认图片路径
        string DefaultImg = "~/ProductImgs/default.jpg";
        public void ProcessRequest(HttpContext context)
        {
            //获取要添加图片的路径
            string path = context.Request.MapPath(IMG + context.Request.QueryString["id"].ToString() + ".jpg");
            Image image;
            //判断图片是否存在
            if (File.Exists(path))
            {
                //加载图片文件
                image = Image.FromFile(path);
                //定义画布
                Graphics graphics = Graphics.FromImage(image);
                //加水印
                graphics.DrawString("马春海的编程博客", new Font("微软雅黑", 12), Brushes.Red, image.Width - 125, image.Height - 15);
                //释放画布
                graphics.Dispose();

            }
            else
            {
                //如果图片不存在的话则显示默认图片
                image = Image.FromFile(DefaultImg);
            }
            //设置输出的图片格式
            context.Response.ContentType = "image/jepg";
            //将修改的图片存入输出流
            image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放图片
            image.Dispose();
            //终止输出
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

3、修改图片路径

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pic.aspx.cs" Inherits="ASP.NET水印._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" ImageUrl="~/ProductImgs/1.jpg" />
        <asp:Image ID="Image2" runat="server" ImageUrl="~/ProductImgs/2.jpg" />
        <asp:Image ID="Image3" runat="server" ImageUrl="~/ProductImgs/3.jpg" />
        <asp:Image ID="Image4" runat="server" ImageUrl="~/ProductImgs/4.jpg" /><br />
        <asp:Image ID="Image5" runat="server" ImageUrl="~/PicHandler.ashx?id=1" />
        <asp:Image ID="Image6" runat="server" ImageUrl="~/PicHandler.ashx?id=2" />
        <asp:Image ID="Image7" runat="server" ImageUrl="~/PicHandler.ashx?id=3" />
        <asp:Image ID="Image8" runat="server" ImageUrl="~/PicHandler.ashx?id=4" />
    </div>
    </form>
</body>
</html>

运行到浏览器的时候:

二、全局Handler方式

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

<httpHandlers>
 <add verb="*" path="ProductImgs/*.jpg" type="PicCoverHandler"/>
    </httpHandlers>

2、PicCoverHandler源码

 public class PicCoverHandler : IHttpHandler
    {
        //默认图片
        private string defaultimg = "~/productimgs/default.jpg";

        public void ProcessRequest(HttpContext context)
        {
            //实例化图片
            Image Cover;
            //判断图片物理路径是否存在
            if (File.Exists(context.Request.PhysicalPath))
            {
                //加载图片
                Cover = Image.FromFile(context.Request.PhysicalPath);
                //定义字体
                Font font = new Font("微软雅黑", 20);
                //定义画布
                Graphics g = Graphics.FromImage(Cover);
                //合成水印图片
                g.DrawString("xiecan.cc", font, Brushes.Blue, Cover.Width - 90, Cover.Height - 30);
                //释放画布
                g.Dispose();
            }
            else
            {
                Cover = Image.FromFile(context.Request.MapPath(defaultimg));
            }
            //定义输出类型
            context.Response.ContentType = "image/jpeg";
            //保存图片到输出流
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放图片
            Cover.Dispose();
            //终止输出
            context.Response.End();

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

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

 

源码下载: 点击下载    密码: jmv4

 

posted @ 2018-04-10 14:11  马春海的编程博客  阅读(138)  评论(0编辑  收藏  举报