请问如何把Sqserver中的图片(image类型)读取出来并显示到Image控件上。求原代码。

解决方案 »

  1.   

    Read to Byte array and instance a image object using the byte array 
      

  2.   

    // Get new file name    string strFullName = dlgFBSave.SelectedPath;    if( strFullName[strFullName.Length - 1] != '\\' )        strFullName += @"\";    strFullName += lsvFileInfo.SelectedItems[0].Text;     string strQuery = "SELECT FileData FROM FileInfo "        + " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();     SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);    DataSet sqlRecordSet = new DataSet();     byte[] bData = null;     //Get file data from DB    try    {        sqlDAdapter.Fill( sqlRecordSet, "FileInfo" );        foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows)        {            if( dr["FileData"] != DBNull.Value )            bData = ( byte[] )dr["FileData"];        }    }    catch(SqlException sqlErr)    {        MessageBox.Show( sqlErr.Message );    }    catch    {        MessageBox.Show( "Failed to read data from DB!" );    }    sqlRecordSet.Dispose();    sqlDAdapter.Dispose();     if( bData != null )    {        // Save file        FileInfo fi = new FileInfo( strFullName );        if( !fi.Exists )        {            //Create the file.            using (FileStream fs = fi.Create())             {                fs.Write( bData, 0, bData.Length);            }        }        else        {            //Create the file.            using (FileStream fs = fi.OpenWrite())             {                fs.Write( bData, 0, bData.Length);            }        }    }
      

  3.   

    上面是读取的方法。
    更详细的,可以看
    http://blog.csdn.net/knight94/archive/2006/03/24/637800.aspx
      

  4.   

    在image.aspx中写
            SqlConnection Conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandText = "SELECT image1 FROM Image1";
            Conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "image");
           byte[] image = (byte[])ds.Tables["image"].Rows[0].ItemArray[0];
           Response.Clear();
           Response.BinaryWrite(image);
    然后把image的scr属性设为image.aspx
      

  5.   

    我是要显示在Image控件里啊,你这个image.aspx是从哪里来的哦,再说Image控件没有scr这个属性啊
      

  6.   

    我照你说的那样做了,但是从数据库中读出来的路径却显示在页面上,而image的src没有得到值 啊