Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi am storing images in sql server. The datatype is Image.

while retrieving the Images from the database i getting error like System.ArgumentException: Parameter is not valid.

The error coming in this line "img = Image.FromStream(ms)".


C#
private void RetriveImage_Click(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection(constr);
           con.Open();
           Image img;
           try
           {
               cmd = new SqlCommand("select Pic from emguimg where ID =" + cbxID.SelectedItem.ToString() + "", con);
               SqlDataReader dr = cmd.ExecuteReader();
               DataTable dt = new DataTable();
               dt.Load(dr);
               byte[] bytes = (byte[])((byte[])dt.Rows[0]["Pic"]);
               MemoryStream ms = new MemoryStream(bytes);
               ms.Write(bytes, 0, bytes.Length);
               img = Image.FromStream(ms);

               PicBox.Image = img;
               PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
               PicBox.BorderStyle = BorderStyle.Fixed3D;
               ms.Flush();
               ms.Close();
}
           catch(Exception ex)
           {
               WriteLogMessage(ex.ToString());
           }
       }
Posted
Updated 28-Jun-15 22:29pm
v2
Comments
Andy Lanng 29-Jun-15 4:32am    
What type is your image?
FromStream expects one of the following:
*.BMP, *.GIF, *.JPEG, *.PNG, *.TIFF
MSVelu8788 29-Jun-15 6:16am    
its JPEG
Sreekanth Mothukuru 29-Jun-15 5:08am    
Two questions here
1. What data do you see in the image column.
2. Why are you type casting twice
MSVelu8788 29-Jun-15 6:35am    
0x53797374656D2E44726177696E672E4269746D6170 <binary data=""> This is data i see in my cloumn and i delete one casting but same error..
Sreekanth Mothukuru 29-Jun-15 7:09am    
Seems like you are not inserting the image properly. Check the code to insert image into database.

1 solution

Most likely, it's where you stored the image: your code is vulnerable to SQL Injection which implies that yoiu always concatenate strings to form SQL commands. Not only does that give your users the chance to damage or destroy your database, it also means that image storage doesn't work. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]

"Hello Griff thanks for your reply. I receive the {byte[21]}"

Yes...and what does that spell out?
[0] 83    S
[1] 121   y
[2] 115   s
[3] 116   t
[4] 101   e

[5] 109   m
[6] 46    .
[7] 68    D
[8] 114   r
[9] 97    a
[10] 119  w
[11] 105  i
[12] 110  n
[13] 103  g
[14] 46   .


So follow the link, read what it says, and pay attention to the Section: "Before I close..."
 
Share this answer
 
v2
Comments
MSVelu8788 29-Jun-15 6:38am    
Hello Griff thanks for your reply. I receive the {byte[21]}
[0] 83
[1] 121
[2] 115
[3] 116
[4] 101

[5] 109
[6] 46
[7] 68
[8] 114
[9] 97
[10] 119
[11] 105
[12] 110
[13] 103
[14] 46
....

but i got the same error
Sreekanth Mothukuru 29-Jun-15 7:09am    
Seems like you are not inserting the image properly. Check the code to insert image into database.
OriginalGriff 29-Jun-15 7:41am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900