ASP.net——水印

本文介绍如何在ASP.NET应用中通过自定义HttpHandler为图片添加水印。具体包括创建HttpHandler的过程、添加水印的代码实现及示例,同时展示了如何通过网页请求调用该Handler来动态显示带水印的图片。

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值