• HttpHandler:给指定路径下的图片添加水印显示


    圣诞节,25日,要交ACCP5.0认证的项目,其中有这样一个要求:书店的所有图书的封面放在了\images\convers\下面,要求所有引用这一路径下的图片都添加书店的店名水印图片。就是说拦截Http请求了,自然想到HttpHandler可以办到。考虑下,实现的效果应该是这样的:为了通用,监视的路径,水印图片路径,默认图片路径3者应该在配置文件里面设定,方便修改;监视路径下的所有图片只要物理存在都要有水印,物理不存在用默认图片替代,若水印图片不存在用文字代替。访问其他路径下的图片应该正常显示没有水印....废话不多说,实现的代码如下:为方便调试,编译httpHandler类的时候要加调试选项,并在项目中引用这个dll(编译:csc /t:library WatermarkHandler.cs /debug)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    namespace xumh
    {
        
    /// <summary>
        
    /// 图片处理模块:给指定路径下的图片添加水印输出
        
    /// 1.监视的目录路径,水印图片路径,默认图片三者都在web.config文件中设定
        
    /// 2.请求的图片的路径不属于监视路径,直接输出图片
        
    /// 3.请求的图片的路径属于监视路径,若图片不存在,显示默认图片,不加水印
        
    /// 4.请求的图片的路径属于监视路径,且文件存在,添加水印
        
    ///        水印图片存在:图片+水印图片,输出
        
    ///        水印图片不存在:图片+水印文字,输出
        
    ///        
        
    /// 5.web.config部分如下:
        
    ///         <appSettings>
        
    ///                  <!--要监视的路径-->
        
    ///                     <add key="monitorPath" value="/images/bookcovers/"/>
        
    ///                 <!--图书封面不存在时显示的默认封面图片的相对路径-->
        
    ///                    <add key="defaultImage" value="images\default.jpg"/>
        
    ///                 <!--相对于物理应用程序的根路径的水印图片的相对路径-->
        
    ///                      <add key="watermarkImage" value="images\WaterMark.jpg"/>
        
    ///        </appSettings>
        
    ///     
        
    ///         <httpHandlers>
        
    ///                <add verb="*" path="*.jpg" type="xumh.WatermarkHandler,WatermarkHandler"/>
        
    ///            </httpHandlers>
        
    ///       许明会  2007-12-23 18:00 Visual Studio 2005 调试通过
        
    /// </summary>

        public class WatermarkHandler:IHttpHandler
        
    {
            
    public bool IsReusable
            
    {
                
    get return true; }
            }


            
    public void ProcessRequest(HttpContext context)
            
    {
                
    string url = context.Request.Url.AbsoluteUri.ToLower();
                
    string monitorPath = System.Configuration.ConfigurationManager.AppSettings["monitorPath"];
                
    //若图片不属于封面,直接输出图片:若图片不存在不做处理
                bool IsInterestUrl = url.Contains(monitorPath);
                System.Drawing.Image imgSource 
    = null;
                
    if (!IsInterestUrl)  
                
    {
                    
    if (!System.IO.File.Exists(context.Request.PhysicalPath))
                        
    return;
                    imgSource 
    = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
                    imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
                    imgSource.Dispose();
                    
    return;
                }
     
                
    //图片属于封面,但图片imageSource不存在,显示默认图片default.jpg
                string physicalPath = context.Request.PhysicalPath;// context.Server.MapPath(url);
                if (!System.IO.File.Exists(physicalPath))
                
    {
                    
    string defaultImage = System.Configuration.ConfigurationManager.AppSettings["defaultImage"];
                    imgSource 
    = System.Drawing.Image.FromFile(context.Request.PhysicalApplicationPath + defaultImage);
                    imgSource.Save(context.Response.OutputStream, imgSource.RawFormat);
                    imgSource.Dispose();
                    
    return;
                }

                
    //--===--------------------只要有封面图片,就为图片添加水印----------------------===---
                string watermarkImage = System.Configuration.ConfigurationManager.AppSettings["watermarkImage"];
                
    string watermarkImagePath = context.Request.PhysicalApplicationPath + watermarkImage;
                
    bool bWatermarkImageExist =System.IO.File.Exists(watermarkImagePath);
                imgSource 
    = System.Drawing.Image.FromFile(physicalPath);
                System.Drawing.Graphics graphic 
    = System.Drawing.Graphics.FromImage(imgSource);
                
    if (bWatermarkImageExist) //“水印图片”存在:存在则封面混合水印图片
                {
                    System.Drawing.Image imgWatermark 
    = System.Drawing.Image.FromFile(watermarkImagePath);
                    graphic.DrawImage(imgWatermark,
                        imgSource.Width 
    - imgWatermark.Width, imgSource.Height - imgWatermark.Height,
                        imgWatermark.Width, imgWatermark.Height);
                    
    //把图片保存到输出流
                    imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imgWatermark.Dispose();
                }

                
    else                               //“水印图片”不存在:输出文本
                {
                    System.Drawing.Font font 
    = new System.Drawing.Font("幼圆"13.0f,System.Drawing.FontStyle.Bold);
                    graphic.DrawString(
    "第三波+书店",font , System.Drawing.Brushes.Red, new System.Drawing.PointF());
                    imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }

                imgSource.Dispose();
                graphic.Dispose();
                
    //标明类型为jpg:如果不标注,IE没有问题,但Firefox会出现乱码
                context.Response.ContentType = "image/jpeg";
                context.Response.Flush();
                context.Response.End();
            }

        }

    }



  • 相关阅读:
    Spring4
    Mysql索引优化、JMM内存模型
    深入浅出JVM
    ZGC垃圾搜集器
    Hibernate4基础
    红楼梦 各版本及资料
    JavaME环境配置
    5.1 Intent
    4.3 异步任务
    4.2.2 网络编程之Socket
  • 原文地址:https://www.cnblogs.com/flaaash/p/1011640.html
Copyright © 2020-2023  润新知