• 图片水印


    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>
  • 相关阅读:
    CSS外边距的合并
    四大主流浏览器内核
    ViewPort视口
    url、src、href定义以及使用区别
    Debug和Release区别
    基线概念
    浅谈C#循环控制流程语句(web前端基础)
    With you With me
    css元素内容基础
    年轻人有梦想,老人有回忆
  • 原文地址:https://www.cnblogs.com/hlh123/p/4705820.html
Copyright © 2020-2023  润新知