用户控件与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页面代码:
效果如下: