使用HttpHandler在Gridview中显示数据库图像

时间:2012-03-05 20:38:37

标签: c# asp.net image httphandler

我正在尝试将ashx页面用作存储在SQL Server数据库中的图像的http处理程序。这些图像将在aspx页面的gridview中显示。

<%@ WebHandler Language="C#" Class="LinqHandler" %>

using System;
using System.Web;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


public class LinqHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection connect = new SqlConnection();
        connect.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT roomID,roomNumber,roomImage1 FROM Rooms " 
                              + "WHERE roomID = @roomID";
        command.CommandType = System.Data.CommandType.Text;
        command.Connection = connect;

        SqlParameter RoomID = new SqlParameter("@roomID", System.Data.SqlDbType.Int);
        RoomID.Value = context.Request.QueryString["roomID"];
        command.Parameters.Add(RoomID);
        connect.Open();
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((byte[])dr["roomImage1"]);
        context.Response.ContentType = "image/gif";
        dr.Close();
        connect.Close();
    }

    public bool IsReusable {
        get {
          return false;
        }
    }
}

由于某种原因,图像不会绑定到aspx页面上的asp:Image控件。我很难过为什么。

以下是数据绑定摘录:

<asp:GridView ID="GridView1" runat="server"
    CssClass="gridviews" PagerStyle-CssClass="pager"
    DataKeyNames="roomID"  AlternatingRowStyle-CssClass="alt"
    AutoGenerateColumns="False"   DataSourceID="SqlDataSource1">
   <Columns>
     <asp:BoundField DataField="roomID" HeaderText="roomID" />
     <asp:BoundField DataField="roomNumber" HeaderText="Room Number" />
     <asp:TemplateField HeaderText="Image 1">
        <ItemTemplate>
          <asp:Image runat="server" ID="pic1" 
              ImageUrl='<%# "~/LinqHandler.ashx?roomID=" + Eval("roomID") %>'>
          </asp:Image>
        </ItemTemplate>
     </asp:TemplateField>
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT [roomID], [roomNumber], [roomImage1] FROM [Rooms]">
</asp:SqlDataSource>

3 个答案:

答案 0 :(得分:2)

您可以将Handler.ashx用于显示图片。例如:

<img src="Handler.ashx?roomID=1" />
通过这种方式,您可以显示一个数字房间的图像。

如果你想以另一种方式做,你可以在css中使用base64。你不需要使用处理程序。例如:

<img runat="server" ID="image"/>

//code behind
var bytes = (byte[])dr["roomImage1"]);
var base64String = System.Convert.ToBase64String(bytes, 0, bytes.Length);
image.src = "data:image/gif;base64,"+ base64String;

编辑:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var bytes = (byte[])((DataRow)e.Row.DataItem)["roomImage1"];
        var base64String = System.Convert.ToBase64String(bytes, 0, bytes.Length);
        var image = (Image)e.Row.FindControl("pic1");
        image.ImageUrl = "data:image/gif;base64,"+ base64String;
        //or
        image.ImageUrl = "LinqHandler.ashx?roomID="+((DataRow)e.Row.DataItem)["roomID"];
    }
}

答案 1 :(得分:1)

尝试如下:

  <ItemTemplate>
    <asp:Image ID="pic1" runat="server" 
        ImageUrl='<%# Eval("roomID", "LinqHandler.ashx?roomID={0}") %>' 
        Height="100px" Width="80px" />
  </ItemTemplate>

答案 2 :(得分:0)

事实证明,我的问题源于将垃圾插入我的SQL BLOB字段。我的源代码中发布的检索机制没有任何问题。如果您想看到我使用的插入语句,请告诉我,我很乐意发布它们。