title: Image Save and Retrieve in SQL Server in asp.net using c#
body: <p>I am trying to save images in a SQL Server database.</p>
  
  <p>I convert the image to bytes and store in SQL Server, but now I want to convert the saved bytes to an image and show it on a label in asp.net using c#.</p>
  
  <p>I tried a lot but didn't find a solution.</p>
  
  <p>Here is the code (convert bytes to Image)</p>
  
  <pre><code>if (fImage.HasFile)
  {
      if (fImage.PostedFile.ContentType == "image/jpg" || fImage.PostedFile.ContentType == "image/jpeg" || fImage.PostedFile.ContentType == "image/png")
      {
         int filelenght = fImage.PostedFile.ContentLength;
         byte[] imagebytes = new  byte[filelenght];
         fImage.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
         SqlCommand cmd = new SqlCommand();
         cmd.CommandText = "Insert into tbImage(Img) values(@img)";
         cmd.Connection = con;
         cmd.Parameters.AddWithValue("@img", imagebytes);
         con.Open();
         cmd.ExecuteNonQuery();
         con.Close();
         Response.Write("Image saved to database");
      }
  }
  </code></pre>