i have stored jpeg images in database table. now i want to retrieve them. here is the code
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.
Loading
VulpesPosted Oct 26, 2011, 11:53 AM
saifullah khanPosted Oct 26, 2011, 12:05 PM
saifullah khanPosted Oct 26, 2011, 11:47 AM
its the output
Javeed M ShaikhPosted Oct 26, 2011, 11:17 AM
Also did you try to add this line in the upload as suggested by Vulpes:
stream.Seek(0, SeekOrigin.Begin);
it should be like this in your code:
FileUpload1.FileContent.Seek(0, SeekOrigin.Begin);
FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
saifullah khanPosted Oct 26, 2011, 11:08 AM
VulpesPosted Oct 26, 2011, 10:53 AM
saifullah khanPosted Oct 26, 2011, 10:41 AM
protected void Button1_Click(object sender, EventArgs e)
{
int len = FileUpload1.PostedFile.ContentLength;
byte[] pic = new byte[len];
FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
cnn.Open();
cmd = new SqlCommand("insert into images values('" + pic + "')", cnn);
cmd.ExecuteNonQuery();
}
here is the code for retriving from db.
protected void Page_Load(object sender, EventArgs e)
{
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);
Image imag =Image.FromStream(stream);
Response.ContentType = "image/gif";
imag.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp);
}
finally
{
cnn.Close();
stream.Close();
}
Javeed M ShaikhPosted Oct 26, 2011, 10:36 AM
what Vulpes is trying to say in his previous post is for you to check if you are doing the following when you are inserting the image in the database:
stream.Seek(0, SeekOrigin.Begin);
Please check the above in your image upload code.
saifullah khanPosted Oct 26, 2011, 10:32 AM
Parameter is not valid.
Image imag =Image.FromStream(stream);
VulpesPosted Oct 26, 2011, 10:15 AM
saifullah khanPosted Oct 26, 2011, 9:59 AM
VulpesPosted Oct 26, 2011, 7:48 AM