csharp,using System.Drawing;,using System.Drawing.Imaging;public void AddWatermark(string imagePath, string watermarkText),{, using (Image image = Image.FromFile(imagePath)), {, using (Graphics graphics = Graphics.FromImage(image)), {, Font font = new Font("Arial", 20, FontStyle.Bold);, SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.White));, PointF point = new PointF(image.Width 150, image.Height 50);, graphics.DrawString(watermarkText, font, brush, point);, }, image.Save("watermarked_image.jpg", ImageFormat.Jpeg);, },},
“这段代码加载一个图像文件,使用Graphics对象在图像上绘制半透明的白色文字作为水印,然后保存修改后的图像。在 ASP.NET 中实现水印功能,通常有两种主要方式:一种是在用户上传图片时添加水印,另一种是通过全局的 HTTP 处理程序在用户请求图片时动态添加水印,以下是这两种方式的具体实现代码示例:
一、在用户上传时添加水印
1、创建 ASP.NET Web 表单
Test2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs" Inherits="Test2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>图片加水印</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="水印文字:" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" Text="添加水印" onclick="Button2_Click" /><br /> <asp:Image ID="Image1" runat="server" /> <asp:Image ID="Image2" runat="server" /> </div> </form> </body> </html>
2、后台代码 Test2.aspx.cs
using System.Web.UI.WebControls; using System.Drawing; using System.Drawing.Imaging; using System.IO; public partial class Test2 : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { HttpPostedFile imgfile = FileUpload1.PostedFile; if (imgfile != null && imgfile.ContentLength > 0) { string filePath = Path.Combine(Server.MapPath("~/image"), "TestImg.jpg"); imgfile.SaveAs(filePath); Image1.ImageUrl = "~/image/TestImg.jpg"; } } protected void Button2_Click(object sender, EventArgs e) { string watermarkText = TextBox1.Text; string imagePath = Path.Combine(Server.MapPath("~/image"), "TestImg.jpg"); string outputPath = Path.Combine(Server.MapPath("~/image"), "TestImg2.jpg"); using (Bitmap map = new Bitmap(imagePath)) { using (Graphics g = Graphics.FromImage(map)) { g.DrawString(watermarkText, new Font("黑体", 14.0f, FontStyle.Bold), Brushes.Blue, new PointF(map.Width 120, map.Height 30)); map.Save(outputPath); } } Image2.ImageUrl = "~/image/TestImg2.jpg"; } }
二、通过 HTTP 处理程序在请求时添加水印
1、创建 HTTP 处理程序类
WaterMark.cs
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; public class WaterMark : IHttpHandler { private const string WATERMARK_URL = "~/Images/watermark.jpg"; // 水印图片路径 private const string DEFAULTIMAGE_URL = "~/Images/default.jpg"; // 默认图片路径(可选) public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { System.Drawing.Image Cover; // 判断请求的文件是否存在 if (File.Exists(context.Request.PhysicalPath)) { // 加载文件 Cover = Image.FromFile(context.Request.PhysicalPath); // 加载水印图片 Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL)); // 创建绘图对象 Graphics g = Graphics.FromImage(Cover); // 在图片上绘制水印 g.DrawImage(watermark, new Rectangle(Cover.Width watermark.Width, Cover.Height watermark.Height, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel); // 释放资源 g.Dispose(); watermark.Dispose(); } else { // 如果文件不存在,加载默认图片(可选) Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL)); } // 设置输出格式为 JPEG context.Response.ContentType = "image/jpeg"; // 将图片保存到输出流 Cover.Save(context.Response.OutputStream, ImageFormat.Jpeg); Cover.Dispose(); context.Response.End(); } }
2、配置 HTTP 处理程序
在web.config
文件中注册 HTTP 处理程序。
web.config
<configuration> <system.web> <httpHandlers> <add verb="" path=".jpg" type="YourNamespace.WaterMark, YourAssemblyName" /> </httpHandlers> </system.web> </configuration>
请将YourNamespace
替换为实际的命名空间,YourAssemblyName
替换为实际的程序集名称。
三、相关问答FAQs
1、问:为什么选择在用户上传时添加水印而不是在请求时添加?
答:在用户上传时添加水印的好处是一旦图片被上传并添加了水印,后续用户每次访问该图片时都不需要再次进行水印处理,减少了服务器的负担,而缺点是原始图片会被修改,如果需要保留原始图片,则需要另外存储一份未加水印的版本,选择哪种方式取决于具体的需求和场景,如果对性能要求较高且不介意修改原始图片,可以选择在上传时添加水印;如果需要保留原始图片的完整性,则可以选择在请求时添加水印。
2、问:在实现水印功能时,如何调整水印的位置和透明度等属性?
答:在上述代码中,可以通过修改Graphics
对象的相关方法来调整水印的位置和透明度等属性,要调整水印的位置,可以修改g.DrawImage
方法中的坐标参数;要调整水印的透明度,可以使用Color
结构的A
(Alpha)分量来设置透明度,然后创建一个带有透明度的Brush
对象用于绘制水印文字或图像,具体的调整方法可以根据实际需求进行尝试和修改。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1622631.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。