ASP.NET网站开发-图片数字水印

HttpModule和HttpHandler

一个请求可以经过多个 Module 但是最终只能被一个 Handler处理

using System;
using System.Web;
public class PicHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

实现数字水印

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(“Cool__King”,font,Brushes.Black,Cover.Width - 90,Cover.Height - 30);
        //释放画布
        g.Dispose();
    }
    else
    {
        Cover = Image.FromFile(context.Request.MapPath(defaulting));
    }
    //设置输出类型为JPEG图片
    context. Response.Cont entType = “image/jpeg”;
    //将修改的图片存入输出流
    Cover.Save(context.Response.Outputstream,System.Drawing.Imaging.ImageFormat.Jpeg);
    //释放图片
    Cover.Dispose();
    //终止输出
    context.Response.End();


猜你喜欢

转载自blog.csdn.net/cool__king/article/details/79871222
今日推荐