// 一般处理程序
<%@WebHandler Language="C#" Class="Tupian1" %>
using System;
using System.Web;
public class TuPian1 : IHttpHandler{
public void RrocessRequest (HttpContext context){
context.Response.ContentType="image/JPEG";
string fullpath=HttpContext.Current.Server.MapPath("....JPG");
using(System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap(fullpath){
using(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmap){
bitmap.Save(context.Response.OutputStream,System.Drawing.Imageing.ImageFormat.Jpeg);
}
}
<%@WebHandler Language="C#" Class="Tupian1" %>
using System;
using System.Web;
public class TuPian1 : IHttpHandler{
public void RrocessRequest (HttpContext context){
context.Response.ContentType="image/JPEG";
string fullpath=HttpContext.Current.Server.MapPath("....JPG");
using(System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap(fullpath){
using(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmap){
if(context.Request.UrlReferrer==null)
{
g.Clear(System.Drawing.Color.White);
g.DrawString("禁止直接浏览,请在页面查看",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
}
}
else if(context.Request.UrlReferrer.Host!="localhost")
{
g.Clear(System.Drawing.Color.White);
g.DrawString("禁止直接浏览,请在页面查看",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
}
bitmap.Save(context.Response.OutputStream,System.Drawing.Imageing.ImageFormat.Jpeg);
}
}
本文介绍了一种使用 ASP.NET 处理图片防盗链的方法。通过检测请求来源,如果来源不符合条件,则在图片上覆盖文字提示,防止图片被非法直接引用。此方法适用于网站运营者保护自己的资源不被盗用。

被折叠的 条评论
为什么被折叠?



