2012-11-30 49 views
0

我在我的Windows窗體中有一個PictureBox控制。
數據類型圖片列中是「形象」表「表名」
這些在這裏是說,從數據庫中取圖像,並把它放在PictureBox控制代碼:無法檢索圖像從數據庫到圖片框

string connectionString = @"Initial Catalog=DataBaseName;Data Source=DataSourceName;Integrated Security=SSPI;"; 
      SqlConnection connection = new SqlConnection(connectionString); 
      connection.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(new SqlCommand("Select Picture From TableName where ID = 2 ", connection)); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      byte[] myImage = new byte[0]; 
      myImage = (byte[])ds.Tables[0].Rows[0]["Picture"]; 
      MemoryStream stream = new MemoryStream(myImage); 
      pictureBox1.Image = Image.FromStream(stream); 
      connection.Close(); 

通常它始終工作,但現在它的顯示ArgumentExeption錯誤'Paramerter is not valid'at this line pictureBox1.Image = Image.FromStream(stream);
我不明白?哪個參數?

任何幫助將不勝感激。

+0

您是否已檢查數據庫查詢是否返回有效結果? – Mate

+0

是的,它顯示有效的結果。我唯一的問題是PictureBox沒有從MemoryStreams對象中獲取圖像。 – shanu

回答

1

它似乎奠定了你保存和從數據庫中讀取對象的方式,異常來自Image.FromStream(stream); METHODE,MSDN說這個例外:

流不具備有效的圖像格式 - 或 - 流爲null。

因此,正如你所提到的,它不是在你的問題無效,我假設你保存數據或以不兼容的方式閱讀它。

+0

我已經檢查過了。 – shanu

+0

你可以嘗試保存文件流到磁盤,並檢查內容是否有效...這裏的示例:http://stackoverflow.com/questions/3879650/create-file-and-save-to-it-using- MemoryStream的。 – Mate

+0

@Mate是的,我試過了,它仍然給出同樣的例外。 – shanu