ASP.NET网站开发--用户控件与HttpHandler

1.用户控件

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

1.1用户控件与页面的区别

后缀名:用户控件以.ascx为后缀,页面后缀是.aspx;

指令:用户控件@Control,页面是@Page;

继承:用户控件继承来自System.Web.Ul.UserControl,而页面继承自System.Web.Ul.Page

访问:用户控件不能直接访问,页面可以;

1.2用户控件与母版页的区别

功能:母版提供一些列页面的页面框架,用户控件提供页面内容中的细节;

母版页:用户控件内容页的框架,每一个页面只能使用一个母版页;

用户控件:用户控件为母版页,内容页,普通页甚至其他用户控件提供内容,且可以使用多个用户控件;

2.模块和处理程序

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

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

Default.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<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>
</html>

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.2使用全局Handler方式实现数字水印

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;
            }
        }
    }
}

Default.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<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>
</html>
Web页面:


在httpHandlers里,添加一段代码:

<add verb="*" path="ProductImgs/*.jpg" type="WebApplication1.Handler1"/>

代码执行结果为:


猜你喜欢

转载自blog.csdn.net/qq_41141741/article/details/79841397