title: How can I resize an image from a database in C#, in good quality
body: <p>I have the following C# code (not mine) which pulls an image out of a database and displays it on a web page:</p>
  
  <pre><code>public partial class Image : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          getFullImage();
      }
  
  
  
      public void getFullImage()
      {
          SqlConnection objCon = new SqlConnection();
          objCon = new SqlConnection(Globals.ConnectionString);
          objCon.Open();
  
          String TmpStr;
          try
          {
  
              byte[] imgData = null;
  
              SqlCommand objCmd = new SqlCommand("Select * from Login where user_Name='" + Request["Id"].ToString() + "'", objCon);
              SqlDataReader reader = objCmd.ExecuteReader();
  
  
              while (reader.Read())
              {
                  HttpContext.Current.Response.Clear();
                  HttpContext.Current.Response.AddHeader("Content-Disposition",
                      "attachment; filename=" + reader["ImageName"]);
                  imgData = (byte[])reader["ImageFile"];
                  TmpStr = reader["ImageFile"].ToString();
                  TmpStr = TmpStr.Substring(1, TmpStr.Length - 1);
              }
              HttpContext.Current.Response.ContentType = "image/jpeg";
              HttpContext.Current.Response.BinaryWrite(imgData);
          }
          catch(Exception ex)
          {
              Response.Write(ex.Message.ToString());
          }
          finally
          {
  
          }
      }
  }
  </code></pre>
  
  <p>I know it's probably really bad practice to store images in a database and use them in this way, but I'm not a hardcore .NET dev and can't rewrite this whole thing (much as I'd love to be able to!).</p>
  
  <p>Anyhow, currently the method above creates full size (often huge) images, which are then resized using CSS, which causes them to degrade terribly in quality.  I wondered if there was anything I could do to resize the image prior to render, so improve quality and reduce load time?</p>
  
  <p>Thanks for any pointers folks...</p>