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

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

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="BindImage.aspx.cs" AutoEventWireup="false" Inherits="ShowImage.BindImg"  %>
None.gif
< HTML >
None.gif 
< HEAD >
None.gif  
< title > BindImg </ title >
None.gif 
</ HEAD >
None.gif 
< body >
None.gif  
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif   
< FONT  face ="宋体" >
None.gif    
< asp:DataGrid  id ="MyDataGrid"  runat ="server"  AutoGenerateColumns ="False"  Width ="632px" >
None.gif     
< AlternatingItemStyle  BackColor ="Beige" ></ AlternatingItemStyle >
None.gif     
< HeaderStyle  HorizontalAlign ="Center" ></ HeaderStyle >
None.gif     
< Columns >
None.gif      
< asp:TemplateColumn  HeaderText ="Photo" >
None.gif       
< ItemTemplate >
None.gif        
< img  src ='<%#  "GetImage.ashx?ID ="+DataBinder.Eval(Container.DataItem," EmployeeID")% > '>
None.gif       
</ ItemTemplate >
None.gif      
</ asp:TemplateColumn >
None.gif      
< asp:BoundColumn  DataField ="LastName"  HeaderText ="Last Name" ></ asp:BoundColumn >
None.gif      
< asp:BoundColumn  DataField ="FirstName"  HeaderText ="First Name" ></ asp:BoundColumn >
None.gif      
< asp:BoundColumn  DataField ="title"  HeaderText ="Title" ></ asp:BoundColumn >
None.gif     
</ Columns >
None.gif    
</ asp:DataGrid ></ FONT >
None.gif  
</ form >
None.gif 
</ body >
None.gif
</ HTML >
None.gif
None.gif
2. BindImage.aspx.cs
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;  using  System.Data.SqlClient; 
None.gif
None.gif
namespace  ShowImage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// 
InBlock.gif 
/// BindImg 的摘要说明。
ExpandedSubBlockEnd.gif 
/// 

InBlock.gif public class BindImage: System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
InBlock.gif 
InBlock.gif  
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
// 在此处放置用户代码以初始化页面
InBlock.gif
   if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    SqlConnection conn 
= new SqlConnection(@"Server=shoutor\mydb;database=northwind;uid=sa;Pwd=shoutor");
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     conn.Open();
InBlock.gif     SqlCommand cmd 
= new SqlCommand("select employeeID,lastname,firstname,title from employees",conn);
InBlock.gif     SqlDataReader reader 
= cmd.ExecuteReader();
InBlock.gif     MyDataGrid.DataSource 
= reader;
InBlock.gif     MyDataGrid.DataBind();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     conn.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ContractedSubBlock.gifExpandedSubBlockStart.gif  
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif  
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif   
//
InBlock.gif
   InitializeComponent();
InBlock.gif   
base.OnInit(e);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{    
InBlock.gif   
this.Load += new System.EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
3. GetImage.ashx
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ WebHandler Language="C#" Class="ShowImage.GetImage" CodeBehind="GetImage.ashx.cs" %>
4. GetImage.ashx.cs
None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.IO; 
None.gif
None.gif
namespace  ShowImage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// 
InBlock.gif 
/// GetImg 的摘要说明。
ExpandedSubBlockEnd.gif 
/// 

InBlock.gif public class GetImage : IHttpHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string id = (string)context.Request["id"];
InBlock.gif   
if(id!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    MemoryStream stream 
= new MemoryStream();
InBlock.gif    SqlConnection conn 
= new SqlConnection(@"Server=;database=;uid=;Pwd=");
InBlock.gif    Bitmap bm 
= null;
InBlock.gif    Image image 
= null;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     conn.Open();
InBlock.gif     SqlCommand cmd 
= new SqlCommand("select photo from employees where employeeid='"+id+"'",conn);
InBlock.gif     
byte[] blob = (byte[])cmd.ExecuteScalar();
InBlock.gif     stream.Write (blob,
78,blob.Length-78);
InBlock.gif     bm 
= new Bitmap(stream);
InBlock.gif
InBlock.gif     
int width=48;
InBlock.gif     
int height = (int)(width*((double)bm.Height/(double)bm.Width));
InBlock.gif
InBlock.gif
InBlock.gif     
// GetThumbnailImage生成缩略图
InBlock.gif
     image = bm.GetThumbnailImage(width,height,null,IntPtr.Zero);
InBlock.gif
InBlock.gif     context.Response.ContentType 
= "image/jpeg";
InBlock.gif
InBlock.gif     image.Save(context.Response.OutputStream,ImageFormat.Jpeg);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(image!=null)
InBlock.gif      image.Dispose();
InBlock.gif     
if(bm!=null)
InBlock.gif      bm.Dispose();
InBlock.gif     stream.Close();
InBlock.gif     conn.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return true;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
  
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

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

来自:http://www.zahui.com/html/4/7991.htm

转载于:https://www.cnblogs.com/Hedonister/archive/2006/03/16/351086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值