Click here to Skip to main content
16,022,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using (MemoryStream ms = new MemoryStream((byte[])SqlDataReader["TestImageColumn"]))
{
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Flush();
ms.Close();
}

i was trying to select an image from database using MemoryStream
i need someone to solve this Issue ,Thanks
Posted
Updated 3-Aug-11 6:46am
v3
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 12:50pm    
Annoying is your code, not exception. In what line?
--SA
Pete O'Hanlon 3-Aug-11 13:15pm    
It's got to be the FromStream line. That's a common exception with this particular method call.
Sergey Alexandrovich Kryukov 3-Aug-11 14:11pm    
Thank you; I understand but all inquirers should specify it if they want help. There are many cases when this is not apparent. Comprehensive exception report is needed.
--SA
snake1 3-Aug-11 13:15pm    
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
Pete O'Hanlon 3-Aug-11 13:23pm    
You've just posted the same line as he already has.

1 solution

Try using the following instead, to validate that the stream data is valid:
C#
pictureBox1.Image = System.Drawing.Image.FromStream(ms, false, true);
 
Share this answer
 
Comments
snake1 3-Aug-11 13:16pm    
i have tried this line and i get the same exception
Pete O'Hanlon 3-Aug-11 13:22pm    
That means that the code thinks it's not a valid image format. Try writing out the contents of the memory stream to a file instead, and see what's in there. Also, save it out to a file, and see if you can open that file with something like Paint.NET.
snake1 3-Aug-11 14:07pm    
is there another way to select image from database and store it into picturebox ?

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