自定义HTTPHandler实现数字水印效果

using  System;
using  System.Web;
using  System.Drawing;
using  System.Drawing.Imaging;
using  System.IO;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// 映射文件后缀名方式的数字水印
/// </summary>

public   class  CoverHandler : IHttpHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
//数字水印路径
    private const string WATERMARK_URL = "~/Images/WaterMark.jpg";
    
//默认图片路径
    private const string DEFAULT_PIC = "~/Images/default.jpg";


    
public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Image Cover;
        
//判断请求的物理路径中,是否存在该文件
        if (File.Exists(context.Request.PhysicalPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//加载文件
            Cover = Image.FromFile(context.Request.PhysicalPath);
            
//加载水印图片
            Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));
            
//实例化画布
            Graphics g = Graphics.FromImage(Cover);
            
//Cover上绘制水印
            g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height, watermark.Width, watermark.Height), 00, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
            
//释放画布
            g.Dispose();
            
//释放水印图片
            watermark.Dispose();
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//加载默认图片
            Cover = Image.FromFile(context.Request.MapPath(DEFAULT_PIC));
        }

        
//设置输出格式
        context.Response.ContentType = "image/jpeg";
        
//将图片存入输出流
        Cover.Save(context.Response.OutputStream,ImageFormat.Jpeg);
        
//释放资源
        Cover.Dispose();
        
//停止HTTP响应
        context.Response.End();
    }


    
public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return false;
        }

    }


}

 

通过实现IHttpHandler接口,自定义实现HTTP请求的处理

希望对于想要了解ASP.NET内部实现机制的朋友给予帮助

转载于:https://www.cnblogs.com/mdy41034264/archive/2009/03/18/1415403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值