日期:2014-05-17  浏览次数:20431 次

如何显示图片?
我数据库中有个image类型的字段,里面存的是图片。现在我想将它取出来显示在页面上该如何做?
小白哈,求高手代码能稍微明确点儿谢了哈。

------解决方案--------------------
探讨
引用:
HTML code

<img src="ashx/GetImage.ashx?ID=1" />


C# code

//ashx:
//sql:select Photo from UserPhoto where ID=@ID
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Op……




……

------解决方案--------------------
你的image字段是二进制流你要有读取二进制流的页面看下面代码:
C# code

public DataRow GetBookDetail(int BookId)
{
string strsql;
DataSet myDs;
try
{
strsql="select BookType.Name as BookTypeName,book.id,book.name,author,price,type,publisher,Description,translator,discount,hits,status,sales,image=case when(not Cover is null) then ' <img src=Readimage.aspx?id='+cast(book.id as varchar(10))+' Border=1 width=80 height=120>' else ' <img src=img/pic.jpg border=1 width=80 height=120>' end from book join BookType on book.type=booktype.id where Book.id="+BookId;
myDs=ExecuteSql4Ds(strsql);
return myDs.Tables[0].Rows[0];
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}

}
//读取二进制流的Readimage.aspx页面代码:
DataView myDv;
int id=int.Parse(Request.QueryString["id"].Trim());
  SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=bookshop;Integrated Security=True");   
  string cmdText = "select cover from book where id="+id+"";
  con.Open();
  SqlDataAdapter sda = new SqlDataAdapter(cmdText,con);
  DataSet ds = new DataSet("ds");
  sda.Fill(ds);
  myDv = ds.Tables[0].DefaultView;
  con.Close();   
    
  try
  {
  Response.ContentType = "image/*";
  Response.BinaryWrite((byte[])myDv[0]["Cover"]);
  }
  catch
  {
  Response.Write("/img/pic.jpg");
  }