0

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.

2
  • do you have checked that the db query is returning a valid result? Commented Nov 30, 2012 at 6:44
  • Yes, it is showing valid results. My only problem is PictureBox is not taking image from the MemoryStreams object. Commented Nov 30, 2012 at 6:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Could you try to save a file stream to disk and check if the content is valid... Example here : stackoverflow.com/questions/3879650/….
When you try to save to file? In this case the problem is the data stored into db... and the solution is not in this piece of code. You need to review the way in which it is stored
@Mate Yes you are right i have checked it on database and it is saving the same byte array in the Picture column for every other image
Great! Now to check that! As a last comment ... I recommend you do not use a dataset if it is extremely necessary to get a picture. Here an example with a SqlDataReader: codeproject.com/Questions/145368/…
@MeNoMore I am using the usual way to store the images in database ` MemoryStream stream = new MemoryStream(); pictureBox1.Image.Save(stream, ImageFormat.Jpeg); byte[] uploadImage = stream.ToArray();`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.