图片水印
1 public class WaterImgHandler:IHttpHandler 2 { 3 4 public bool IsReusable 5 { 6 get { return false; } 7 } 8 9 public void ProcessRequest(HttpContext context) 10 { 11 // 指定水印图片 12 string water_Url = "~/wImages/logo.gif"; 13 // 指定默认图片 14 string default_Url = "~/wImages/error.jpg"; 15 // 创建图片对象,用于存放用户读取的图片 16 System.Drawing.Image g; 17 // 判断所请求的图片是否存在,存在就给图片加水印 18 if (File.Exists(context.Request.PhysicalPath)) 19 { 20 g = System.Drawing.Image.FromFile(context.Request.PhysicalPath); 21 // 加载水印图片 22 System.Drawing.Image waterImg = System.Drawing.Image.FromFile(context.Request.MapPath(water_Url)); 23 Graphics gh = Graphics.FromImage(g); 24 gh.DrawImage(waterImg, new RectangleF(0, g.Height - waterImg.Height, 100, 20)); 25 26 // 加载水印字符串 27 Font font = new System.Drawing.Font("Arial", 6, System.Drawing.FontStyle.Bold); 28 gh.DrawString("abcdefg", font, Brushes.White, 2, 2); 29 gh.Dispose(); 30 waterImg.Dispose(); 31 } 32 else { 33 // 图片不存在,显示默认图片 34 g = System.Drawing.Image.FromFile(context.Request.MapPath(default_Url)); 35 } 36 context.Request.ContentType = "image/jpeg"; 37 g.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); 38 g.Dispose(); 39 context.Response.End(); 40 41 } 42 } 43 // 最后在配置文件Web.config中写 44 <httpHandlers> 45 <add verb="*" path="*.jpg" type="Picture.WaterImgHandler"/> 46 </httpHandlers>