ASPNET图片水印防盗链技术实现细节如何?
- 行业动态
- 2025-03-08
- 1
在ASP.NET中,可以通过以下代码实现图片加水印和防盗链功能:“
csharp,using System;,using System.Drawing;,using System.Drawing.Imaging;,using System.IO;,using System.Web;public class ImageHandler : IHttpHandler,{, public void ProcessRequest(HttpContext context), {, string imagePath = context.Server.MapPath("~/images/sample.jpg");, string watermarkText = "Watermark";, int fontSize = 20;, string fontName = "Arial";, Font font = new Font(fontName, fontSize);, Color textColor = Color.White; using (Image image = Image.FromFile(imagePath)), using (Graphics graphics = Graphics.FromImage(image)), {, SizeF textSize = graphics.MeasureString(watermarkText, font);, Point position = new Point((image.Width (int)textSize.Width) / 2, (image.Height (int)textSize.Height) / 2);, graphics.DrawString(watermarkText, font, new SolidBrush(textColor), position); context.Response.ContentType = "image/jpeg";, image.Save(context.Response.OutputStream, ImageFormat.Jpeg);, }, } public bool IsReusable => false;,},
` 此代码通过
Graphics`对象在图片上绘制水印文本,并将处理后的图片输出到HTTP响应流中。
在ASP.NET中实现图片加水印和防盗链功能,可以有效保护你的图片资源不被未经授权的使用,下面是一个详细的实现步骤和代码示例:
创建ASP.NET Web项目
创建一个新的ASP.NET Web应用程序项目。
添加图片处理类
创建一个类来处理图片的加水印和防盗链逻辑,这个类将包含两个主要方法:一个用于加水印,另一个用于检查请求来源是否合法。
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; public class ImageHandler { private const string WatermarkText = "YourWatermark"; private const string AllowedHost = "yourdomain.com"; public byte[] AddWatermark(byte[] imageBytes) { using (var image = Image.FromStream(new MemoryStream(imageBytes))) { // Create a graphics object from the image using (Graphics g = Graphics.FromImage(image)) { // Set the properties for the watermark text Font font = new Font("Arial", 20, FontStyle.Bold); Brush brush = Brushes.White; Point position = new Point(image.Width / 4, image.Height / 4); // Draw the watermark text onto the image g.DrawString(WatermarkText, font, brush, position); } // Save the image with watermark to a memory stream using (var ms = new MemoryStream()) { image.Save(ms, image.RawFormat); return ms.ToArray(); } } } public bool IsRequestValid() { string referer = HttpContext.Current.Request.Headers["Referer"]; if (!string.IsNullOrEmpty(referer)) { Uri refererUri = new Uri(referer); return refererUri.Host == AllowedHost; } return false; } }
配置HTTP处理程序
在Global.asax
文件中,配置一个HTTP处理程序来拦截对图片资源的请求,并应用加水印和防盗链逻辑。
using System.IO; using System.Web; public class Global : HttpApplication { protected void Application_BeginRequest(Object sender, EventArgs e) { string filePath = HttpContext.Current.Request.PhysicalPath; if (filePath.EndsWith(".jpg") || filePath.EndsWith(".png")) { ImageHandler handler = new ImageHandler(); if (!handler.IsRequestValid()) { HttpContext.Current.Response.StatusCode = 403; // Forbidden HttpContext.Current.Response.End(); } else { byte[] imageBytes = File.ReadAllBytes(filePath); byte[] watermarkedImage = handler.AddWatermark(imageBytes); HttpContext.Current.Response.BinaryWrite(watermarkedImage); HttpContext.Current.Response.End(); } } } }
部署和测试
将项目部署到服务器上,然后访问图片资源以验证加水印和防盗链功能是否正常工作,你可以通过修改浏览器的Referer头信息来测试防盗链功能。
以下是一个简单的表格,展示了不同请求情况下的处理结果:
请求情况 | Referrer | 处理结果 |
合法请求 | yourdomain.com | 返回加水印的图片 |
非规请求 | otherdomain.com | 返回403错误 |
无Referer | 返回403错误 |
相关问答FAQs
Q1: 如果我想更改水印的位置或样式,应该怎么做?
A1: 你可以通过修改ImageHandler
类中的position
变量来调整水印的位置,或者通过修改字体、颜色等属性来改变水印的样式,你可以将position
设置为new Point(image.Width / 2, image.Height / 2)
来将水印居中显示。
Q2: 如何确保我的图片资源不被其他网站盗用?
A2: 除了使用Referer头进行简单的防盗链检查外,你还可以考虑使用更复杂的验证机制,如基于令牌的验证或IP白名单,确保你的服务器配置正确,限制跨域请求,并使用HTTPS来加密传输数据,以防止中间人攻击。