vishnu suresh

vishnu suresh

  • NA
  • 53
  • 16.2k

How to convert image in binary format to image format

Dec 30 2014 6:32 AM

I have a code to store images directly to the database in binary format. But I cannot retrieve the image. This is my image uploading code.


 

if(FileUpload2.HasFile)



{


if (FileUpload2.PostedFile.ContentType == "image/jpg" || FileUpload2.PostedFile.ContentType == "image/jpeg" || FileUpload2.PostedFile.ContentType == "image/png")



{


int filelength = FileUpload2.PostedFile.ContentLength;


byte[] imagebytes = new byte[filelength];



FileUpload2.PostedFile.InputStream.Read(imagebytes, 0, filelength);


SqlCommand cmd = new SqlCommand();


//cmd.CommandText = "Insert into login(image) values(@image)";


//cmd = new SqlCommand("update login set image=@image where uname=@uname", con);


cmd.CommandText = "update login set image=@image where uname=@uname";


cmd.Parameters.AddWithValue("@uname", TextBox1.Text);



cmd.Connection = con;


cmd.Parameters.AddWithValue("@image", imagebytes);



con.Open();


cmd.ExecuteNonQuery();


con.Close();


Response.Write("Image saved to database");





}



Answers (6)