我正在尝试从SQL Server 2008 Express中检索图像并将其插入<ASP:Image>
。我尝试使用MemoryStream
,但我似乎无法做到正确。
我现在的C#代码如下:
try
{
con.Open();
SqlCommand sqlGetStep1 = new SqlCommand("staff_getStep1", con);
{
sqlGetStep1.CommandType = CommandType.StoredProcedure;
sqlGetStep1.Parameters.Add(new SqlParameter("@taskID", Convert.ToInt16(taskID)));
SqlDataReader step1 = sqlGetStep1.ExecuteReader();
//Check if username exists
if (step1.Read())
{
step1Text = (string)step1["step1Text"];
step1Image = (byte)step1["step1Image"];
}//if
else
{
step1Text = "null";
step1Image = 0;
}//else
}//sqlDeleteNotification
}//try
catch (SqlException sqlEx)
{
lblSQLError.Visible = true;
lblSQLError.Text = sqlEx.Message.ToString();
}//catach sql
catch (Exception ex)
{
lblError.Visible = true;
lblError.Text = ex.ToString();
}//Catch exception
finally
{
con.Close();
}//Finally
我希望显示图像的aspx代码如下:
<asp:Panel runat="server" ID="pnlStep1" Visible="false" CssClass="NormalText">
<asp:Label runat="server" ID="lblStep1Text" Text="Step 1 Instruction: "></asp:Label>
<asp:TextBox runat="server" ID="txtStep1Text" Text="" ReadOnly="true"></asp:TextBox>
<asp:Label runat="server" ID="lblStep1Image" Text="Step 1 Image: "></asp:Label>
<asp:Image runat="server" ID="imgStep1" ImageUrl="" Height="100px" Width="100px"/>
</asp:Panel>
任何帮助都将非常感谢:)
答案 0 :(得分:2)
您需要有一个单独的网址,用于获取要传递的图像的ID(在本例中为步骤ID),并将内容作为具有正确MIME类型的结果传递。使用此方法的网址作为asp:Image
的网址。通常,您将(在WebForms中)实现为HttpHandler。见How to return an image as the response in ASP.NET