Click here to Skip to main content
15,889,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have stored jpeg images in database table. Now I want to retrieve them. here is the code
C#
MemoryStream stream = new MemoryStream();
try
{
    cnn.Open();
    cmd = new SqlCommand("select pics from images", cnn);
    byte[] image = (byte[])cmd.ExecuteScalar();
   
    stream.Write(image, 0, image.Length);
    Bitmap bitmap = new Bitmap(stream);
    Response.ContentType = "image/gif";
    bitmap.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Bmp);
}
finally
{
    cnn.Close();
    stream.Close();
}

but when i run the application it gives me the following error
Parameter is not valid.
Bitmap bitmap = new Bitmap(stream);

can somebody tells me what i need to correct.
Posted
Updated 26-Oct-11 0:43am
v2
Comments
Slacker007 26-Oct-11 6:44am    
Edit: formatting

1 solution

Try rewinding the stream, before you use it. Write leaves the stream at the end, so try
C#
stream.Seek(0,0);
 
Share this answer
 
Comments
Manfred Rudolf Bihy 26-Oct-11 6:48am    
Sounds reasonable! 5+
:)
Nagy Vilmos 26-Oct-11 8:11am    
Clever, get's my 5. :)
saifullahiit 26-Oct-11 10:00am    
i have used it but it gives the same error

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