下面利用ashx文件可以方便实现从数据库中读取图片并显示在datagrid当中
1.BindImage.aspx
<%
@ Page language="c#" Codebehind="BindImage.aspx.cs" AutoEventWireup="false" Inherits="ShowImage.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
='<%#
"GetImage.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
>
![None.gif](/Images/OutliningIndicators/None.gif)
2.
BindImage.aspx.cs
using
System;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Data.SqlClient;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
ShowImage
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// BindImg 的摘要说明。
///
public class BindImage: System.Web.UI.Page
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection conn = new SqlConnection(@"Server=shoutor\mydb;database=northwind;uid=sa;Pwd=shoutor");
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select employeeID,lastname,firstname,title from employees",conn);
SqlDataReader reader = cmd.ExecuteReader();
MyDataGrid.DataSource = reader;
MyDataGrid.DataBind();
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Close();
}
}
}
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
3.
GetImage.ashx
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ WebHandler Language="C#" Class="ShowImage.GetImage" CodeBehind="GetImage.ashx.cs" %>
4.
GetImage.ashx.cs
using
System;
using
System.Web;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.IO;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
ShowImage
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// GetImg 的摘要说明。
///
public class GetImage : IHttpHandler
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void ProcessRequest(HttpContext context)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string id = (string)context.Request["id"];
if(id!=null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MemoryStream stream = new MemoryStream();
SqlConnection conn = new SqlConnection(@"Server=;database=;uid=;Pwd=");
Bitmap bm = null;
Image image = null;
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
int width=48;
int height = (int)(width*((double)bm.Height/(double)bm.Width));
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
// GetThumbnailImage生成缩略图
image = bm.GetThumbnailImage(width,height,null,IntPtr.Zero);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
context.Response.ContentType = "image/jpeg";
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
image.Save(context.Response.OutputStream,ImageFormat.Jpeg);
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(image!=null)
image.Dispose();
if(bm!=null)
bm.Dispose();
stream.Close();
conn.Close();
}
}
}
public bool IsReusable
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
}
}
ProcessRequest使用了空架类库的易用的Image.GetThumbnailImage方法来把位图缩小到宽度为48像素,同时保持图象的长宽比。可以使用类似的技术来创建显示来自其他数据库图象的DataGrid。基本的思想是使用模板列来输出一个引用某个HTTP处理句柄的<img>标签,并在查询字符串中包含唯一标识图片所在的记录的信息。之后,HTTP处理句柄使用ADO.NET来获取图象数据位,并使用GDI+(图象设备接口+)来构建图象。
来自:http://www.zahui.com/html/4/7991.htm