一般给图片加水印时,采用一般处理程序来处理。 public class PhotoHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; //if (!System.IO.File.Exists(context.Request.PhysicalPath)) //{ // context.Response.WriteFile(context.Server.MapPath("~/Images/error.jpg")); // return; //} string path = "";//绝对路径 //System.Drawing.Image.FromFile('计算出图片的绝对路径'); System.Drawing.Image img = System.Drawing.Image.FromFile(path); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img)) { // 取得图片的分辨率, 每英寸的像素数 float res = img.HorizontalResolution; // 取得图片的大小, 以像素为单位 int width = img.Width; // 计算出字大小 float fontsize = (float)(width / 20.0) / res * 72; // 创建字体 System.Drawing.Font font = new System.Drawing.Font("Arial", fontsize); string smg = "CDSN"; // 准备输出文本的矩形区域,用来在区域中设置对齐方式 System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, img.Width, img.Height); System.Drawing.StringFormat format = new System.Drawing.StringFormat(); // 行中的左右,即水平的左右对齐方式 format.Alignment = System.Drawing.StringAlignment.Far; // 垂直对齐方式 format.LineAlignment = System.Drawing.StringAlignment.Far; // 字符串的方向 DirectionVertical 为垂直方向 // 默认为 System.Drawing.StringFormatFlags.DirectionRightToLeft 左右方向 format.FormatFlags = System.Drawing.StringFormatFlags.DirectionVertical; g.DrawString(smg, font, System.Drawing.Brushes.Blue, rect, format); font.Dispose(); } img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } public bool IsReusable { get { return false; } } }