当前位置:编程学习 > asp >>

ASP.NET 图片加水印防盗链

首先建一个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1:IHttpHandler //调用接口
{
 public Class1()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    public bool IsReusable
    {
        get { return true; }
    }
    public void ProcessRequest(HttpContext context)
    {
        HttpRequest req = context.Request;
        if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)  //反盗链代码判断
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
            System.Drawing.Graphics g = Graphics.FromImage(img);
            g.DrawString("三国演义", new Font("宋体", 20, FontStyle.Bold), Brushes.White, 10, 10);
            img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.Flush();
            context.Response.End();
        }
        else www.zzzyk.com
        {
            context.Response.Write("您不能盗链本站图片");
        }
    }
}
在web.config中注册接口:
<httpHandlers>
      <add verb="*" path="images/*.jpg" type="Class1,App_Code"/>
    </httpHandlers>
 

摘自 淡蓝蓝蓝

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,