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

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


实现方法

Step1:实现动态生成图片。首先,为类库 mylib.system.web 添加对 System.Drawing 的引用。然后,编写如下代码根据传给 MyHandler.jxd 的参数动态生成一个图片,并写到 Response 的 OutputsStream 里面。MyHandler.cs的代码如下
MyHandler.cs
using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace mylib.system.web
{
    
public class MyHandler : System.Web.IHttpHandler
    {
        
#region IHttpHandler 成员

        
public bool IsReusable
        {
            
get { return false; }
        }

        
public void ProcessRequest(System.Web.HttpContext context)
        {
            
// 获取参数n,并将其转换为Int型。
            string n = context.Request.QueryString["n"];
            
int count = 0;
            
int.TryParse(n, out count);

            
// 使用GDI+函数画一个长方形。
            Bitmap bmp = new Bitmap(count, 20);
            Graphics graphic 
= Graphics.FromImage(bmp);
            MemoryStream stream 
= new MemoryStream();
            SolidBrush brush 
= new SolidBrush(Color.SteelBlue);

            
try
            {    
                graphic.FillRectangle(brush, 
00, 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
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">
            
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            
<Columns>
                
<asp:TemplateField HeaderText="网址">
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[0]") %>'></asp:Label>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="访问量">
                    
<ItemTemplate>
                        
<asp:Image ID="Image1" runat="server" ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>' />
                        
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[1]") %>'></asp:Label>
                    
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
            
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            
<AlternatingRowStyle BackColor="#DCDCDC" />
        
</asp:GridView>
        
    
</div>
    
</form>
</body>
</html>

Step3:作一些测试数据,并绑定到 GridView 中。
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

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

        GridView1.DataSource 
= data;
        GridView1.DataBind();
    }
}

下载本篇全部源代码

本篇到此结束,下一篇将介绍让Web程序与HTTP处理程序共享Session的方法。

本系列共6篇文章
    实战 HTTP 处理程序(HTTP Handler) (6)——条码随意打
    实战 HTTP 处理程序(HTTP Handler) (5)——不用临时文件,直接打开动态生成的文件
    实战 HTTP 处理程序(HTTP Handler) (4)——与Web程序共享Session
    实战 HTTP 处理程序(HTTP Handler) (3)——动态生成图片   <- you are here.
    实战 HTTP 处理程序(HTTP Handler) (2)——向HTTP 处理程序传递参数   
    实战 HTTP 处理程序(HTTP Handler) (1)——创建一个最简单的 HTTP Handler  

 


转载于:https://www.cnblogs.com/1-2-3/archive/2007/09/01/878246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值