图片添加水印 并在IIS上正常显示

public class ImageHandler:IHttpHandler   
{   
    public ImageHandler()   
    {   
        //   
        // TODO: 在此处添加构造函数逻辑   
        //   
    }  
 
 
    #region IHttpHandler 成员   
  
    public bool IsReusable   
    {   
        get { return true; }   
    }   
  
    public void ProcessRequest(HttpContext context)   
    {   
        //throw new Exception("The method or operation is not implemented.");   
         //获取请求的物理图片路径   
        string imagePath = context.Request.PhysicalPath;   
        Bitmap image = null;   
        if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存   
        {   
            image = new Bitmap(imagePath);   
            image = AddWaterMark(image);   
            context.Cache[imagePath] = image;   
        }   
        else//否则就直接从混存中取出添加了水印的图片,节省时间   
        {   
            image = context.Cache[imagePath] as Bitmap;   
        }   
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中   
    }  
    #endregion   
  
    private Bitmap AddWaterMark(Bitmap image)   
    {   
        string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();   
        int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());   
        Font font = new Font("宋体", fontSize);   
  
        //Brush brush = Brushes.DarkGray;   
        Brush brush = Brushes.Red;   
        Graphics g = Graphics.FromImage(image);   
        SizeF size = g.MeasureString(text, font);   
        g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);   
        g.Dispose();   
        return image;   
    }   
}  
public class ImageHandler:IHttpHandler
{
 public ImageHandler()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }

    #region IHttpHandler 成员
    public bool IsReusable
    {
        get { return true; }
    }
    public void ProcessRequest(HttpContext context)
    {
        //throw new Exception("The method or operation is not implemented.");
         //获取请求的物理图片路径
        string imagePath = context.Request.PhysicalPath;
        Bitmap image = null;
        if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
        {
            image = new Bitmap(imagePath);
            image = AddWaterMark(image);
            context.Cache[imagePath] = image;
        }
        else//否则就直接从混存中取出添加了水印的图片,节省时间
        {
            image = context.Cache[imagePath] as Bitmap;
        }
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
    }
    #endregion
    private Bitmap AddWaterMark(Bitmap image)
    {
        string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
        int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
        Font font = new Font("宋体", fontSize);
        //Brush brush = Brushes.DarkGray;
        Brush brush = Brushes.Red;
        Graphics g = Graphics.FromImage(image);
        SizeF size = g.MeasureString(text, font);
        g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
        g.Dispose();
        return image;
    }
}
参照: http://blog.csdn.net/zhoufoxcn/archive/2008/01/10/2033530.aspx
 view plaincopy to clipboardprint?
<appSettings>   
               <add key="WaterMark" value="动态添加水印的内容"/>   
    <add key="font-size" value="20"/>   
</appSettings>   
  
<httpHandlers>   
    <add path="*.jpg,*.bmp"  verb="*" type="ImageHandler"/>   
               //可以制定路径 path="image/*.jpg"   
</httpHandlers>  
 <appSettings>
                <add key="WaterMark" value="动态添加水印的内容"/>
  <add key="font-size" value="20"/>
 </appSettings>
 <httpHandlers>
  <add path="*.jpg,*.bmp"  verb="*" type="ImageHandler"/>
                //可以制定路径 path="image/*.jpg"
 </httpHandlers> 
在本地测试正常 可是在IIS上图片就没有正常显示
这时:在网站上目录上右击,选择属性,选择主目录,配置,
随便选择列表中的一行,点击编缉,复制执行文件的路径,然后关闭,再点击添加,可执行文件用粘贴就可以了,后缀名写上.jpg,点击确定。按此方法再添加其他后缀名
 
本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/hei25hei/archive/2010/08/10/5801497.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值