简体   繁体   English

无法将图像从数据库检索到图片框

[英]Unable to retrieve image from database to picturebox

I have a PictureBox control in my Windows Form. 我的Windows窗体中有一个PictureBox控件。
Datatype of Picture column is 'image' in table 'TableName' 表“ TableName”中“图片”列的数据类型为“图像”
These here is the code which says, take the image from database and put it in PictureBox control: 这些是下面的代码,它表示从数据库中获取图像并将其放入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();

Usually it always works but now its showing a ArgumentExeption with error 'Paramerter is not valid' at this line pictureBox1.Image = Image.FromStream(stream); 通常它总是可以工作,但是现在在此行显示错误为“ Paramerter无效”的ArgumentExeption pictureBox1.Image = Image.FromStream(stream);
I don't understand? 我听不懂 Which Parameter? 哪个参数?

Any help will be appreciated. 任何帮助将不胜感激。

It seems to lay on the way you save and read the object from the database, the exception is comming from Image.FromStream(stream); 似乎是从数据库保存和读取对象的方式上出现的,异常来自Image.FromStream(stream); MEthode, MSDN say about this exception: MEthode, MSDN谈到了此异常:

The stream does not have a valid image format -or- stream is null. 该流没有有效的图像格式-或者-stream为null。

So as you mentioned its not Null in your question i assume youre saving the data or reading it in a uncompatible way. 因此,正如您在问题中提到的不是Null一样,我假设您保存数据或以不兼容的方式读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM