使用GridView绑定图片的方法,不需要在服务器端生成图片

很多人开始有这个疑问,GridView控件中的ImageField没有DataField属性,那么如何才能绑定到SQL Server中的Image Field?自从DynamicImage控件从beta2中消失后,这就成了个问题。但是,ASP.NET2.0随之也给我们带来了另外一种解决方案,那就是方便地利用HttpHandler(.ashx)动态显示数据库中的图片,这点在VS2005中提供了PersonalWebSite等模版中已经给出方案:通过ashx动态获取数据库中的某条图片数据,然后在GridView等控件的自定义模版中安置一个Image控件,并设置Image控件的ImageUrl属性为类似 XXX.ashx?photoId=1 即可显示图片。
<% @ WebHandler Language = " C# "  Class = " GetPicture "   %>

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

public   class  GetPicture : IHttpHandler  {
    
    
public void ProcessRequest (HttpContext context) {
        context.Response.ContentType 
= "image/jpeg";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput 
= false;
        
        Stream stream 
= null;

        
if (context.Request.QueryString["PhotoID"!= null &&
            context.Request.QueryString[
"PhotoID"!= "")
        
{
            
string photoId = context.Request.QueryString["PhotoID"];
            stream 
= GetPhoto(photoId);
        }

        
else
            
return;

        
const int buffersize = 1024 * 16;
        
byte[] buffer = new byte[buffersize];
        
if (stream == null || stream.Length < 1)
            
return;            
        
int count = stream.Read(buffer, 0, buffersize);
        
while (count > 0){
            context.Response.OutputStream.Write(buffer, 
0, count);
            count 
= stream.Read(buffer, 0, buffersize);
        }



    }

    
    
public Stream GetPhoto(string fileTypeID){
        SqlConnection myConnection 
= new SqlConnection("Integrated Security=SSPI;Initial Catalog=WebWindows;Data Source=guosong");
        SqlCommand myCommand 
= myConnection.CreateCommand();
        myCommand.CommandText 
= "GetFileTypeIcon";
        myCommand.CommandType 
= System.Data.CommandType.StoredProcedure;

        myCommand.Parameters.Add(
new SqlParameter("@fileTypeID", fileTypeID));
        myConnection.Open();
        
object result = myCommand.ExecuteScalar();
        
        
try{
            
return new MemoryStream((byte[])result);
        }

        
catch (ArgumentNullException e){
            
return null;
        }

        
finally{
            myConnection.Close();
        }

    }


 
    
public bool IsReusable {
        
get {
            
return false;
        }

    }


}

 

 <asp:TemplateField HeaderText="ICON">
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetPicture.ashx?PhotoID=" + Eval("FileTypeID") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值