前言
我们如何创建一个datagrid列,显示从数据库中获得的图像?
这是一个经常被问及的关于datagrid控件的问题,而且其他可以很容易地通过结合你已经知道的关于模板列的内容以及一点点关于http处理句柄(http handler)的知识来回答。
下面使用northwind数据库的employees表来在一个datagrid中显示数据库中的图像。
代码
— bindimg.aspx
<%@ page language="c#" codebehind="bindimg.aspx.cs" autoeventwireup="false" inherits="showimg.bindimg" %>
<html>
<head>
<title>bindimg</title>
</head>
<body>
<form id="form1" method="post" runat="server">
<font face="宋体">
<asp:datagrid id="mydatagrid" runat="server" autogeneratecolumns="false" width="632px">
<alternatingitemstyle backcolor="beige"></alternatingitemstyle>
<headerstyle horizontalalign="center"></headerstyle>
<columns>
<asp:templatecolumn headertext="photo">
<itemtemplate>
<img src=<%# "getimg.ashx?id="+databinder.eval(container.dataitem,"employeeid")%>>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn datafield="lastname" headertext="last name"></asp:boundcolumn>
<asp:boundcolumn datafield="firstname" headertext="first name"></asp:boundcolumn>
<asp:boundcolumn datafield="title" headertext="title"></asp:boundcolumn>
</columns>
</asp:datagrid></font>
</form>
</body>
</html>
— bindimg.aspx.cs
using system;
using system.data;
using system.drawing;
using system.web;
using system.data.sqlclient;
namespace showimg
{
/// <summary>
/// bindimg 的摘要说明。
/// </summary>
public class bindimg : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid mydatagrid;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!page.ispostback)
{
sqlconnection conn = new sqlconnection(@"server=shoutor\mydb;database=northwind;uid=sa;pwd=shoutor");
try
{
conn.open();
sqlcommand cmd = new sqlcommand("select employeeid,lastname,firstname,title from employees",conn);
sqldatareader reader = cmd.executereader();
mydatagrid.datasource = reader;
mydatagrid.databind();
}
finally
{
conn.close();
}
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
— getimg.ashx
<%@ webhandler language="c#" class="showimg.getimg" %>
— getimg.aspx.cs
using system;
using system.web;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.drawing.imaging;
using system.io;
namespace showimg
{
/// <summary>
/// getimg 的摘要说明。
/// </summary>
public class getimg : ihttphandler
{
public void processrequest(httpcontext context)
{
string id = (string)context.request["id"];
if(id!=null)
{
memorystream stream = new memorystream();
sqlconnection conn = new sqlconnection(@"server=shoutor\mydb;database=northwind;uid=sa;pwd=shoutor");
bitmap bm = null;
image image = null;
try
{
conn.open();
sqlcommand cmd = new sqlcommand("select photo from employees where employeeid="+id+"",conn);
byte[] blob = (byte[])cmd.executescalar();
stream.write (blob,78,blob.length-78);
bm = new bitmap(stream);
int width=48;
int height = (int)(width*((double)bm.height/(double)bm.width));
// getthumbnailimage生成缩略图
image = bm.getthumbnailimage(width,height,null,intptr.zero);
context.response.contenttype = "image/jpeg";
image.save(context.response.outputstream,imageformat.jpeg);
}
finally
{
if(image!=null)
image.dispose();
if(bm!=null)
bm.dispose();
stream.close();
conn.close();
}
}
}
// 实现成员接口(system.web.ihttphandler.isreusable)
public bool isreusable
{
get
{
return true;
}
}
}
}
总结
作为额外的补充,processrequest还使用了空架类库的易用的image.getthumbnailimage方法来把位图缩小到宽度为48像素,同时保持图象的长宽比。可以使用类似的技术来创建显示来自其他数据库图象的datagrid。基本的思想是使用模板列来输出一个引用某个http处理句柄的<img>标签,并在查询字符串中包含唯一标识图片所在的记录的信息。之后,http处理句柄使用ado.net来获取图象数据位,并使用gdi+(图象设备接口+)来构建图象。
