简体   繁体   中英

Unable to retrieve image from database to picturebox

I have a PictureBox control in my Windows Form.
Datatype of Picture column is 'image' in table 'TableName'
These here is the code which says, take the image from database and put it in PictureBox control:

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);
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); MEthode, MSDN say about this exception:

The stream does not have a valid image format -or- stream is null.

So as you mentioned its not Null in your question i assume youre saving the data or reading it in a uncompatible way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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