我试图使图像从数据库中出现在Web窗体上的图像按钮上。没有错误消息,但图像未显示。只有一小的小,表明出现在图像按钮,但没有从数据库中的图像....以网络形式在图像按钮上显示数据库中的图像
我的处理程序是在这里:
public void ProcessRequest (HttpContext context)
{
Int32 Member_No;
if (context.Request.QueryString["id"] != null)
{
Member_No = Convert.ToInt32(context.Request.QueryString["id"]);
context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(Member_No);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
else
{
context.Response.Write("No Image Found");
}
}
public bool IsReusable
{
get
{
return false;
}
}
public Stream ShowEmpImage(int Member_No)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select Photo from Members where Member_No = @ID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", Member_No);
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
con.Close();
}
}
}
图像本身是否可以通过IIS访问?可能是由于权限限制了您的应用程序显示图像。 –
我通常从Context.Response.Clear()开始,因为我总是偏执于某些(未计划的)可能已经在输出缓冲区中。 – tgolisch
我不是什么问题,因为如果我使用url调用haddler:MemberImage.ImageUrl =“http:// localhost:56394/CallMarkSite/Pages/Handler.ashx?id =”+ searchBox.Text;它工作得很好 –