1、创建HttpHandler
先添加ASP.NET3.5应用程序
添加新建项,在web中添加一般处理程序

2、PicHandler.ashx源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
namespace ASP.NET水印
{
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 - 50, 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();
}
3、修改图片路径
<%@ 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>
4.使用全局Handler方式实现数字水印
Default.aspx页面代码:
<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" />
<asp:Image ID="Image5" runat="server" ImageUrl="~/ProductImgs/default.jpg" />
</div>
</form>
</body>
Handler1.ashx页面:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
namespace ASP.NET水印
{
/// <summary>
/// PicCoverHandler 的摘要说明
/// </summary>
public class PicCoverHandler : IHttpHandler
{
string DefaultImg = "~/ProductImgs/default.jpg";
public void ProcessRequest(HttpContext context)
{
string path = context.Request.PhysicalPath;
Image image;
if (File.Exists(path))
{
image = Image.FromFile(path);
Graphics graphics = Graphics.FromImage(image);
//加水印
graphics.DrawString("劍笑", new Font("黑体", 12), Brushes.Blue, image.Width - 50, 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();
}
Web页面配置
显示效果:


版权声明:https://blog.youkuaiyun.com/fateeric/article/details/79925295