结合ashx来在DataGrid中显示从数据库中读出的图片

结合ashx来在DataGrid中显示从数据库中读出的图片

hx后缀名的文件请看 消除图片在ie中缓存而无法更新的问题 相关文章

从数据库中读取和保存图片请看 写入和读取图片(c# asp。net sqlserver) 相关文章

下面利用ashx文件可以方便实现从数据库中读取图片并显示在datagrid当中

//---------------------------------------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>

//------------------------------------BindImage.aspx.cs ---------------------------------------------

P>using System;

using System.Data;

using System.Drawing;

using System.Web; using System.Data.SqlClient;

namespace ShowImage

{

///

/// BindImg 的摘要说明。

///

public class BindImage: 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);

}

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}

//-----------------------------GetImage.ashx ---------------------------------------

<%@ WebHandler Language="C#" Class="ShowImage.GetImage" CodeBehind="GetImage.ashx.cs" %>

//--------------------------------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;

namespace ShowImage

{

///

/// GetImg 的摘要说明。

///

public class GetImage : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

string id = (string)context.Request["id"];

if(id!=null)

{

MemoryStream stream = new MemoryStream();

SqlConnection conn = new SqlConnection(@"Server=;database=;uid=;Pwd=");

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();

}

}

}

public bool IsReusable

{

get

{

return true;

}

}

}

}

ProcessRequest使用了空架类库的易用的Image.GetThumbnailImage方法来把位图缩小到宽度为48像素,同时保持图象的长宽比。可以使用类似的技术来创建显示来自其他数据库图象的DataGrid。基本的思想是使用模板列来输出一个引用某个HTTP处理句柄的<img>标签,并在查询字符串中包含唯一标识图片所在的记录的信息。之后,HTTP处理句柄使用ADO.NET来获取图象数据位,并使用GDI+(图象设备接口+)来构建图象。

posted on   秋天  阅读(350)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2008年7月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示