显示图像从二进制数据/Blob在asp.net

本文关键字:Blob asp net 数据 显示图 图像 二进制 显示 | 更新日期: 2023-09-27 17:50:18

我已经设法从使用imagehandler数据库检索我的图像,但问题是,它只使用第一个图像检索,而不是在不同的数据上显示不同的图像。

这是我的图像处理器代码我对这行(string volunteerNames = imageRequest.QueryString["VolunteerName"];)有问题,它只显示System.Byte[],而不是显示名称

<%@ WebHandler Language="C#" Class=".ShowImage" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
    HttpRequest imageRequest = context.Request;
    HttpResponse imageResponse = context.Response;

    //string connStr = ConfigurationManager.ConnectionStrings["DBconnStr"].ConnectionString;
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = "Data Source=.''SQLEXPRESS;AttachDbFilename='"C:''Users''User''Desktop''ProjectAV2.9''ProjectA''ProjectA''App_Data''ProjectA.mdf'";Integrated Security=True;Connect Timeout=30;User Instance=True";
        string queryStr = "SELECT * FROM TVolunteer WHERE VolunteerName = @Name";
        string bQueryStr = "SELECT * FROM BVolunteer WHERE VolunteerName = @Name";
        string volunteerNames = imageRequest.QueryString["VolunteerName"];
        SqlCommand bCMD = new SqlCommand(bQueryStr, conn);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        bCMD.Parameters.AddWithValue("@Name", volunteerNames);
        cmd.Parameters.AddWithValue("@Name", volunteerNames);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Read();
            string volunteerNRIC = dr["VolunteerNRIC"].ToString();
            byte[] volunteerProfilePicture = (byte[])dr["VolunteerProfilePicture"];
            string volunteerName = dr["VolunteerName"].ToString();
            string volunteerContact = dr["VolunteerContact"].ToString();
            string volunteerAddress = dr["VolunteerAddress"].ToString();
            string volunteerEmail = dr["VolunteerEmail"].ToString();
            imageResponse.BinaryWrite(volunteerProfilePicture);
        }
        dr.Close();
        SqlDataReader bDR = bCMD.ExecuteReader();
        if (bDR.HasRows)
        {
            bDR.Read();
            byte[] volunteerProfilePicture = (byte[])bDR["VolunteerProfilePicture"];
            string volunteerName = bDR["VolunteerName"].ToString();
            string volunteerContact = bDR["VolunteerContact"].ToString();
            string volunteerAddress = bDR["VolunteerAddress"].ToString();
            string volunteerEmail = bDR["VolunteerEmail"].ToString();
            imageResponse.BinaryWrite(volunteerProfilePicture);
            bDR.Close();
        }
    }
}
 public bool IsReusable
 {
    get
    {
        return false;
    }
 }
}

这是我的数据列表

  <asp:DataList ID="dl_tvolunteer" runat="server" CellPadding="3" 
        ForeColor="#333333" RepeatColumns="3"
    RepeatDirection="Horizontal" Width="375px" Height="193px" GridLines="Both">
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "VolunteerProfilePicture","~/ShowImage.ashx?VolunteerName="+ Eval("VolunteerName") %>'  />
            <br />
            <asp:Label ID="lbl_Name" runat="server" Text='<%# Bind("VolunteerName") %>'></asp:Label>
            <br />
            <asp:Label ID="lbl_Contact" runat="server" Text='<%# Bind("VolunteerContact") %>'></asp:Label>
            <br />
            <asp:Label ID="lbl_Address" runat="server" Text='<%# Bind("VolunteerAddress") %>'></asp:Label>
            <br />
            <asp:Label ID="lbl_Email" runat="server" Text='<%# Bind("VolunteerEmail") %>'></asp:Label>
        </ItemTemplate>
    </asp:DataList>

显示图像从二进制数据/Blob在asp.net

你应该在你的图像处理程序中定义你的mime类型:

imageResponse.ContentType = "Image/Jpeg";

EDIT:看起来你应该像那样改变你的图像列。它获取志愿者的名字并传递给查询字符串参数:

<asp:Image ID="Image1" runat="server" ImageUrl='<%# 
       DataBinder.Eval(Container.DataItem, 
                       "VolunteerName", 
                       "~/ShowImage.ashx?VolunteerName={0}") 
       %>'/>