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

解决方案 »

  1.   

     void GetImageSrcFromDB()
     2        {
     3            string strImageID = Request.QueryString["id"];
     4            SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=sa;");
     5            SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" 
     6                + strImageID, myConnection);
     7
     8            try
     9            {
    10                myConnection.Open();
    11                SqlDataReader myDataReader;
    12                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    13                if(myDataReader.Read())
    14                {
    15                    Response.Clear();
    16
    17                    //Response.ContentType = myDataReader["PersonImageType"].ToString();
    18                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
    19                }
    20                myConnection.Close();
    21            }
    22            catch (SqlException SQLexc)
    23            {
    24                Response.Write(SQLexc.ToString());
    25            }
    26            //Response.End();
    27        }
      

  2.   

    数据库中读出图片并显示在picturebox中:private void ShowImage(string sql)
         {
         //调用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'");
         SqlCommand cmd = new SqlCommand(sql, conn);
         conn.Open();
         byte[] b= (byte[])cmd.ExecuteScalar();
         if (b.Length 〉 0)
         {
         MemoryStream stream = new MemoryStream(b, true);
         stream.Write(b, 0, b.Length);
         pictureBox1.Image = new Bitmap(stream);
         stream.Close();
         }
         conn.Close();
         }
      

  3.   

    如果我用  image 显示怎么弄啊?
      

  4.   

    <img src="ashx/GetImage.ashx?ID=1" />//ashx:
            //sql:select Photo from UserPhoto where ID=@ID
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            byte[] b= (byte[])cmd.ExecuteScalar();
            context.Response.BinaryWrite(b);
            context.Response.ContentType = "image/jpeg";
            context.Response.End();
      

  5.   


    star   Tim 要我让你把代码写的能看懂点儿、、、就是说  。 <img src="ashx/GetImage.ashx?ID=1" />
      这句啥子意思
      

  6.   

    督察?
    就是去请求GetImage.ashx,通过ID返回你的image字段的值。然后输出图片的二进制。
      

  7.   

    你的image字段是二进制流你要有读取二进制流的页面看下面代码: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");
      }