`

实战 HTTP 处理程序(HTTP Handler) -- 动态生成图片

阅读更多
在前两篇文章中,我们已经创建了一个HTTP处理程序,并实现了页面到HTTP处理程序之间的字符串参数的传递。下面让我们来作个比较实用的东西--一个类似于 Google Analytics 的GridView,实现后的效果见下图。


实现方法

Step1:实现动态生成图片。首先,为类库 mylib.system.web 添加对 System.Drawing 的引用。然后,编写如下代码根据传给 MyHandler.jxd的参数动态生成一个图片,并写到 Response 的 OutputsStream 里面。MyHandler.cs的代码如下
MyHandler.cs
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;

usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;

namespacemylib.system.web
{
publicclassMyHandler:System.Web.IHttpHandler
{
#regionIHttpHandler成员

publicboolIsReusable
{
get{returnfalse;}
}

publicvoidProcessRequest(System.Web.HttpContextcontext)
{
//获取参数n,并将其转换为Int型。
stringn=context.Request.QueryString["n"];
intcount=0;
int.TryParse(n,outcount);

//使用GDI+函数画一个长方形。
Bitmapbmp=newBitmap(count,20);
Graphicsgraphic
=Graphics.FromImage(bmp);
MemoryStreamstream
=newMemoryStream();
SolidBrushbrush
=newSolidBrush(Color.SteelBlue);

try
{
graphic.FillRectangle(brush,
0,0,count,20);

//将画好的图形保存到内存中。
bmp.Save(stream,ImageFormat.Png);

//将内存中的图片发送到客户端
context.Response.ContentType="image/png";
context.Response.OutputStream.Write(stream.ToArray(),
0,(int)stream.Length);
}
finally
{
//释放资源
stream.Close();
brush.Dispose();
brush
=null;
graphic.Dispose();
graphic
=null;
}
}
#endregion
}
}

Step2:在Web程序的 Default.aspx 中放置一个 GridView 控件。并为其添加两个模板列。一列用于显示“网址”;另一列中放一个 Image 控件和一个 Label 控件,用于显示访问量。Label 控件直接绑定到数组下标为1的字段,就不用多说了。Image 控件通过“ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>'”这样的绑定表达式向 MyHandler.jxd 发送带参数的请求,Myhandler.cs 将根据参数 n 的值生成相应大小的图片并发送给 Image 控件。

Default.aspx
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="3"GridLines="Vertical"BackColor="White"BorderColor="#999999"BorderStyle="None"BorderWidth="1px">
<FooterStyleBackColor="#CCCCCC"ForeColor="Black"/>
<Columns>
<asp:TemplateFieldHeaderText="网址">
<ItemTemplate>
<asp:LabelID="Label1"runat="server"Text='<%#Eval("[0]")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="访问量">
<ItemTemplate>
<asp:ImageID="Image1"runat="server"ImageUrl='<%#"~/MyHandler.jxd?n="+Eval("[1]")%>'/>
<asp:LabelID="Label1"runat="server"Text='<%#Eval("[1]")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyleBackColor="#EEEEEE"ForeColor="Black"/>
<SelectedRowStyleBackColor="#008A8C"Font-Bold="True"ForeColor="White"/>
<PagerStyleBackColor="#999999"ForeColor="Black"HorizontalAlign="Center"/>
<HeaderStyleBackColor="#000084"Font-Bold="True"ForeColor="White"/>
<AlternatingRowStyleBackColor="#DCDCDC"/>
</asp:GridView>

</div>
</form>
</body>
</html>

Step3:作一些测试数据,并绑定到 GridView 中。
Default.aspx.cs
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;

usingSystem.Collections.Generic;

publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
List
<string[]>data=newList<string[]>();
data.Add(
newstring[]{"1.使用分治法实现的全排列算法","50"});
data.Add(
newstring[]{"2.让Ruby的数组支持任意起始下标","16"});
data.Add(
newstring[]{"3.输出二叉树的方法","14"});
data.Add(
newstring[]{"4.让ruby以矩阵的样式输出二维数组","9"});
data.Add(
newstring[]{"5.算法分析涉及到的一些函数图像","8"});

GridView1.DataSource
=data;
GridView1.DataBind();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics