Hello Everyone,
I've stored the images in my database and now i want to display those images in my web page and i'm using the code given below for displaying it.....
But the compiler throwing an error
Invalid Parameter at Bitmap bitmap =new Bitmap(stream) line
plz try to understand my code and suggest me some way to come out of this.......
MemoryStream stream = new MemoryStream ();
SqlConnection connection = new SqlConnection (@"server=.;database=kiritbhai;uid=sa;pwd=");
try
{
connection.Open ();
SqlCommand command = new SqlCommand ("Select ImageFile from Rough_Images", connection);
byte[] image = (byte[]) command.ExecuteScalar ();
stream.Write (image, 0, image.Length);
Bitmap bitmap= new Bitmap (stream);
Response.ContentType = "image/gif";
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
}
finally
{
connection.Close ();
stream.Close ();
}
MananPosted Nov 23, 2007, 11:54 PM
--> Here is the code to store image in database:
FileStream fs = new FileStream("C:\\sunset.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, (int)fs.Length);
SqlConnection cn = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=test;server=MPS-7\\SQLEXPRESS");
cn.Open();
SqlCommand cmd = new SqlCommand("insert into testimg values(@t1)", cn);
cmd.Parameters.AddWithValue("@t1", picbyte);
cmd.ExecuteNonQuery();
cn.Close();
--> Here is the code to retrieve the image from the database:
SqlConnection cn = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=test;server=MPS-7\\SQLEXPRESS");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from testimg", cn);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds=new DataSet();
da.Fill(ds,"testimg");
byte[] picbyte = new byte[16];
picbyte = (byte[])ds.Tables[0].Rows[1][0];
MemoryStream ms = new MemoryStream();
ms.Write(picbyte, 0, picbyte.Length);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
I hope this will help you.
kishore yPosted Nov 23, 2007, 3:15 PM
hi..
I am also facing the same problem.Even I am having the problem when I am attempting to store them into database.Help me out.
Thanking u,
Kishore.
sai babuPosted Oct 12, 2007, 9:01 AM
yes