用户控件与HttpHandler

用户控件与HttpHandler

一、用户控件

        简单的来说,用户控件就是能够在其中放置标记和Web服务器的容器,可以被看作一个独立的单元,拥有自己的属性和方法,并且可以被放入到ASPX页面上。应为它的工作方式跟ASP.NET很相似,也可以理解:当一个Web窗体被当作Server控件使用时,这个Web窗体便是用户控件。

    1.创建用户控件

        要创建新的用户控件,首先应在Web站点中添加一个新的“Web用户控件”文件。


    2.使用用户控件

        打开页面,从工具箱里直接拉控件直接使用。下面是简单的页面。


二、模块和处理程序

    模块和处理程序就是,在网站中我们基于著作权的保护,对站点中提供的图片添加网站的标识。

    1.使用指定Handler方式实现数字水印

        Default.aspx页面代码:
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Image ID="Image1" runat="server" ImageUrl="~/ProductImgs/1.jpg" />  
        <asp:Image ID="Image2" runat="server" ImageUrl="~/ProductImgs/2.jpg" />  
        <asp:Image ID="Image3" runat="server" ImageUrl="~/ProductImgs/3.jpg" />  
        <asp:Image ID="Image4" runat="server" ImageUrl="~/ProductImgs/4.jpg" />  
        <asp:Image ID="Image9" runat="server" ImageUrl="~/ProductImgs/default.jpg" />  
    </div>  
    <div>  
        <asp:Image ID="Image5" runat="server" ImageUrl="~/Handler1.ashx?id=1" />  
        <asp:Image ID="Image6" runat="server" ImageUrl="~/Handler1.ashx?id=2" />  
        <asp:Image ID="Image7" runat="server" ImageUrl="~/Handler1.ashx?id=3" />  
        <asp:Image ID="Image8" runat="server" ImageUrl="~/Handler1.ashx?id=4" />  
    </div>  
    </form>  
</body>  

        Handler1.ashx页面:

    

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Drawing;  
using System.Drawing.Imaging;  
using System.IO;  
  
namespace WebApplication1  
{  
    /// <summary>  
    /// Handler1 的摘要说明  
    /// </summary>  
    public class Handler1 : IHttpHandler  
    {  
  
        //private string IMG = "~/ProductImgs/";  
        public void ProcessRequest(HttpContext context)  
        {           
           Image Cover;  
           string Path = context.Request.MapPath(IMG + context.Request.Params["id"]+".jpg");  
  
            if (File.Exists(Path))  
            {  
                Cover = Image.FromFile(Path);  
                Graphics g = Graphics.FromImage(Cover);  
                g.DrawString("xiecan.cc", new Font("宋体",20), Brushes.Red,Cover.Width-90,Cover.Height-20);  
                g.Dispose();  
            }  
            else  
            {  
                Cover = null;  
            }  
            context.Response.ContentType = "image/jpeg";  
            Cover.Save(context.Response.OutputStream, ImageFormat.Jpeg);  
            Cover.Dispose();  
            context.Response.End();  
        }  
  
        public bool IsReusable  
        {  
            get  
            {  
                return false;  
            }  
        }  
    }  
}  

    2.使用全局Handler方式实现数字水印

        Default.aspx页面代码:

<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Image ID="Image1" runat="server" ImageUrl="~/ProductImgs/1.jpg" />  
        <asp:Image ID="Image2" runat="server" ImageUrl="~/ProductImgs/2.jpg" />  
        <asp:Image ID="Image3" runat="server" ImageUrl="~/ProductImgs/3.jpg" />  
        <asp:Image ID="Image4" runat="server" ImageUrl="~/ProductImgs/4.jpg" />  
        <asp:Image ID="Image5" runat="server" ImageUrl="~/ProductImgs/default.jpg" />  
    </div>  
    </form>  
</body>  

         Handler1.ashx页面:

        

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.IO;  
using System.Drawing;  
using System.Drawing.Imaging;  
  
namespace WebApplication1  
{  
    /// <summary>  
    /// Handler1 的摘要说明  
    /// </summary>  
    public class Handler1 : IHttpHandler  
    {  
        private string IMGS = "/~ProductImgs/";  
        public void ProcessRequest(HttpContext context)  
        {  
            Image img;  
            string path = context.Request.PhysicalPath;   
            if (File.Exists(path))  
            {  
                img=Image.FromFile(path);  
                Graphics graphics = Graphics.FromImage(img);  
                graphics.DrawString("版权所有",new Font("宋体",20),Brushes.Red,img.Width-50,img.Height-20);  
                graphics.Dispose();  
            }  
            else  
            {  
                img = null;  
            }  
            context.Request.ContentType = "image/jpeg";  
            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);  
            img.Dispose();  
            context.Response.End();  
        }  
  
        public bool IsReusable  
        {  
            get  
            {  
                return false;  
            }  
        }  
    }  
}  

    Web页面代码:

    

    效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

思丰百年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值