ASP.NET网站开发(HttpHandler)

    HttpHandler     

      一、 Httphandler技术是指既方便又不破坏原始图片,只是在服务器发送图片带客户端前做一些处理,动态的添加上数字水印效果的技术。

       二、HttpHandler与HttpModule的区别:在HTTP请求的处理过程中,只能调用一个HttpHandler,但可以调用多个HttpModule;一个请求可已经过多个Module最终只能被一个Handler处理。

       三、  HttpHandler概述:打开VisualStudio创建一个ASP.NET  Web 应用程序,在“添加”“新建项”中添加“一般处理程序”,如图所示

单击“添加”以后创建了一个以.ashx为后缀的文件。默认内容为:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

         四、   封面数字水印的实现(指定Handler方式)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace 第2章_用户控件与HttpHandler
{
    /// <summary>
    /// PicHandler 的摘要说明
    /// </summary>
    public class PicHandler : IHttpHandler
    {

        //普通图片的虚拟路径
        private const string IMG = "~/productimgs/";
        //默认图片的虚拟路径
        private string defaultimg = "~/productimgs/default.jpg";

        //处理方法
        public void ProcessRequest(HttpContext context)
        {
            //定义新的图片
            Image Cover;
            //获取图片物理路径
            string path = context.Request.MapPath(IMG + context.Request.Params["id"].ToString() + ".jpg");
            //判断图片的物理路径是否存在
            if (File.Exists(path))
            {
                //加载图片
                Cover = Image.FromFile(path);
                //定义字体
                Font font = new Font("微软雅黑", 20);
                //定义画布
                Graphics g = Graphics.FromImage(Cover);
                //合成水印图片
                g.DrawString("xiecan.cc", font, Brushes.Red, Cover.Width - 90, Cover.Height - 30);
                //释放画布
                g.Dispose();
            }
            else
            {
                //加载默认图片
                Cover = Image.FromFile(context.Request.MapPath(defaultimg));
            }
            //定义输出类型
            context.Response.ContentType = "image/jpeg";
            //保存图片到输出流
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放画布
            Cover.Dispose();
            //输出结束
            context.Response.End();

        }

        //是否重用
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

        五、数字水印的实现(全局Handler方式)

        1.先修改Web.config文件,在文件中添加以下代码

 <httpHandlers>
        <add verb="*" path="ProductImgs/*.jpg" type="PicCoverHandler"/>
 </httpHandlers>

        2.创建一个实现了HttpHandler的类PicCoverHandler,此类代码与上面方法代码相似,只不过不再获取id值了,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace 第2章_用户控件与HttpHandler
{
    /// <summary>
    /// PicCoverHandler 的摘要说明
    /// </summary>
    public class PicCoverHandler : IHttpHandler
    {
        //默认图片
        private string defaultimg = "~/productimgs/default.jpg";


        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            //实例化图片
            Image Cover;
            //判断图片物理路径是否存在
            if (File.Exists(context.Request.PhysicalPath))
            {
                //加载图片
                Cover = Image.FromFile(context.Request.PhysicalPath);
                //定义字体
                Font font = new Font("微软雅黑", 20);
                //定义画布
                Graphics g = Graphics.FromImage(Cover);
                //合成水印图片
                g.DrawString("xiecan.cc", font, Brushes.Black, Cover.Width - 90, Cover.Height - 30);
                //释放画布
                g.Dispose();
            }
            else
            {
                Cover = Image.FromFile(context.Request.MapPath(defaultimg));
            }
            //定义输出类型
            context.Response.ContentType = "image/jpeg";
            //保存图片到输出流
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放图片
            Cover.Dispose();
            //终止输出
            context.Response.End();

        }
    }
}
    注意:使用这种配置方法,在开发服务器上运行时没有任何问题,可将网站部署到IIS上运行时,会没有任何效果。

                



猜你喜欢

转载自blog.csdn.net/mogul1/article/details/79918269