Answered by:
Parameter is not valid.

Question
-
hi:
I receive this error when i try to load image from ms(memorystream object)
i use this code
dr=com2.ExecuteReader();
if (dr.Read())
{
byte[] pic_arr =(byte[])dr["img"];
MemoryStream ms = new MemoryStream(pic_arr);
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = Image.FromStream(ms);
}the error from the last line point to ms
the data is from sql column(image format) stored by this code
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
????????????????
Thursday, June 13, 2013 7:25 PM
Answers
-
thanks Christopher84
I replaced the insertion code
SqlCommand com = new SqlCommand("insert into imge1 (name,img) values ('" + textBox1.Text + "','" + pictur_arr + "')", cn);
whith this implenentation
SqlCommand com = new SqlCommand("insert into imge1 (name,img) values (@name,@img)", cn); com.Parameters.AddWithValue("@name", textBox1.Text); com.Parameters.AddWithValue("@img", pictur_arr);
and its working now
- Edited by ameer.ahmd Monday, June 17, 2013 9:27 AM
- Marked as answer by Jason Dot Wang Thursday, June 20, 2013 6:11 AM
Monday, June 17, 2013 9:21 AM
All replies
-
It is propably telling you that the byte array does not contain any (valid) iamge information. It might even be null.
What image do format did you expect pic_arr to contain? And have you checked if it contains any data at all? It does not seems to be null (wich would throw a null exception), but it might still contain an array of lenght 0 or other stuff that cannot be made into an image class.
How is the data saved into the Database in the first place?
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2
Thursday, June 13, 2013 7:40 PM -
I would avoid using memory stream. The pointers don't work the way you would expect them to. Can you use another type stream besides the memory stream class?
jdweng
Thursday, June 13, 2013 7:42 PM -
I have used memoryStream before to get byte arrays from function that only "write" to streams. I once even had to wrap a memory stream in a filestream, because of some very wierd parameter requirements.
And have used memoryStream before to read data from byte arrrays into functions that only accept streams (it was the same appliaction, but webservice and consumer).
Granted, I never needed to use Seek. It is implied that the readonly memory stream* is already at the beginning. So maybe you can just cut that line?
*MemoryStreams created from byte arrays are implicitly read only.
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2
Thursday, June 13, 2013 7:59 PM -
thanks for reply this is the code i use to store data in sql database MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] pictur_arr = new byte[ms.Length]; ms.Position = 0; ms.Read(pictur_arr, 0, pictur_arr.Length); SqlCommand com = new SqlCommand("insert into imge1 (name,img) values ('" + textBox1.Text + "','" + pictur_arr + "')", cn); cn.Open(); int n = com.ExecuteNonQuery(); if (n > 0) MessageBox.Show("image has been saved successfuly"); cn.Close(); i tried to cut the seek but no change????
- Edited by ameer.ahmd Thursday, June 13, 2013 8:26 PM
Thursday, June 13, 2013 8:21 PM -
Codeblocks please:
MemoryStream ms = new MemoryStream();
How about using MemoryStream.ToArray() to get the contents of the MemoryStream? What you do right now looks overly complex and prone to error.
pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] pictur_arr = new byte[ms.Length]; ms.Position = 0;
ms.Read(pictur_arr, 0, ictur_arr.Length); SqlCommand com = new SqlCommand("insert into imge1 (name,img) values ('" + textBox1.Text + "','" + pictur_arr + "')", cn); cn.Open(); int n = com.ExecuteNonQuery(); if (n > 0)
MessageBox.Show("image has been saved successfuly"); cn.Close()
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2
- Edited by Christopher84 Thursday, June 13, 2013 8:32 PM
Thursday, June 13, 2013 8:32 PM -
Let's first check if the process at all can work. Execute this code in a Click Event:
MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); //make the array the only piece to hold the image data byte[] pictur_arr = ms.toArray(); ms= null; pictureBox1.Image = null; //try to recreate the image and display it ms = new MemoryStream(pictur_arr); pictureBox1.Image = Image.FromStream(ms);
If this code works, something goes wrong during storage and/or retrieval into DB.
I also checked the Documentation of Image.FromStream(), and it is only know to throw ArgumentInvalidExceptions - if either the streams data does not makes sense as image, or the Data is null.
About stroing binary data into a DB:
The Datatype of the Column should be varbinary or binary. Image is deprecated and to be removed in later releases.
In order to transmit it properly, you cannot just throw a byte[] it into the Query String. In fact you should never build a query using string conaction either, you should always use SQL-Parameters. Not only do they add typesafety, they also immunize against SQL-Injections:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
Here is a list of allowed SqlDBTypes:
http://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2
- Proposed as answer by Jason Dot Wang Thursday, June 20, 2013 6:11 AM
Friday, June 14, 2013 9:19 AM -
thanks Christopher84
I replaced the insertion code
SqlCommand com = new SqlCommand("insert into imge1 (name,img) values ('" + textBox1.Text + "','" + pictur_arr + "')", cn);
whith this implenentation
SqlCommand com = new SqlCommand("insert into imge1 (name,img) values (@name,@img)", cn); com.Parameters.AddWithValue("@name", textBox1.Text); com.Parameters.AddWithValue("@img", pictur_arr);
and its working now
- Edited by ameer.ahmd Monday, June 17, 2013 9:27 AM
- Marked as answer by Jason Dot Wang Thursday, June 20, 2013 6:11 AM
Monday, June 17, 2013 9:21 AM -
Hi,
Welcome to MSDN Forum Support.
Thank you for sharing your solution with us. Appreciate this active participation.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
Thursday, June 20, 2013 6:12 AM