不使用通用http处理程序显示二进制图像

时间:2014-10-17 04:50:53

标签: c# asp.net database image

我必须在我的网页上显示学生姓名和图像,同时从下拉列表中选择学生ID。图像以db二进制格式存储在db上。如何检索图像并在图像框上显示。下面给出的代码仅显示学生的名字和姓氏。如何在不使用http通用处理程序页面的情况下显示图像?请帮帮我。

代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet1TableAdapters.TextBoxTableTableAdapter tx;
        tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
        DataTable dt = new DataTable();
        dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));

        foreach (DataRow row in dt.Rows)
        {
            TextBox1.Text = (row["FirstName"].ToString());
            TextBox2.Text = (row["SecondName"].ToString());
        }
    }

SQL查询:

SELECT FirstName, SecondName, StudentImage FROM TextBoxTable WHERE (Id = @Id)

Aspx来源:

<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" />
</div>

数据库:

enter image description here

1 个答案:

答案 0 :(得分:1)

代码

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataSet1TableAdapters.TextBoxTableTableAdapter tx;
    tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
    DataTable dt = new DataTable();
    dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));
    foreach (DataRow row in dt.Rows)
    {
        TextBox1.Text = (row["FirstName"].ToString());
        TextBox2.Text = (row["SecondName"].ToString());  
        byte[] barrImg = (byte[])(row["StudentImage"].ToString());
        string base64String = Convert.ToBase64String(barrImg , 0, barrImg.Length);
        Image1.ImageUrl = "data:image/png;base64," + base64String;
    }
}'

我认为此代码适用于您