[英]Unable to retrieve image from database to picturebox
我的Windows窗體中有一個PictureBox
控件。
表“ TableName”中“圖片”列的數據類型為“圖像”
這些是下面的代碼,它表示從數據庫中獲取圖像並將其放入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();
通常它總是可以工作,但是現在在此行顯示錯誤為“ Paramerter無效”的ArgumentExeption
pictureBox1.Image = Image.FromStream(stream);
我聽不懂 哪個參數?
任何幫助將不勝感激。
似乎是從數據庫保存和讀取對象的方式上出現的,異常來自Image.FromStream(stream); MEthode, MSDN談到了此異常:
該流沒有有效的圖像格式-或者-stream為null。
因此,正如您在問題中提到的不是Null一樣,我假設您保存數據或以不兼容的方式讀取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.