title: Resizing Image Quality Reduction
body: <p>I have written a simple PhotoEditor helper class to downscale and crop images uploaded to my website. Everything is working but I am seeing unsatisfactory quality when the image is saved to file. I have read up on the different settings you can tweak below is my setup for resizing, cropping is identical.</p>
  
  <pre><code>    public Image ResizeImage(Image imgToResize, Size size)
      {
          int sourceWidth = imgToResize.Width;
          int sourceHeight = imgToResize.Height;
  
          float nPercentW = (size.Width/(float) sourceWidth);
          float nPercentH = (size.Height/(float) sourceHeight);
  
          float nPercent = nPercentH &lt; nPercentW ? nPercentH : nPercentW;
  
          var destWidth = (int) (sourceWidth*nPercent);
          var destHeight = (int) (sourceHeight*nPercent);
  
          var src = imgToResize;
  
          using (var dst = new Bitmap(destWidth, destHeight, imgToResize.PixelFormat))
          {
              dst.SetResolution(imgToResize.HorizontalResolution, imgToResize.VerticalResolution);
  
              using (var g = Graphics.FromImage(dst))
              {
                  var mime = GetMimeType(imgToResize);
                  ImageFormat format;
                  if (mime == "image/gif" || mime == "image/png")
                  {
                      //convert all gif to png, better resize quality
                      g.SmoothingMode = SmoothingMode.AntiAlias;
                      g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                      g.DrawImage(src, 0, 0, dst.Width, dst.Height);
                      format = ImageFormat.Png;
                  }
                  else
                  {
                      //jpeg
                      g.CompositingQuality = CompositingQuality.HighQuality;
                      g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                      g.SmoothingMode = SmoothingMode.HighQuality;
                      g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                      format = ImageFormat.Jpeg;
                  }
                  g.DrawImage(src, 0, 0, dst.Width, dst.Height);
  
                  // At this point the new bitmap has no MimeType
                  // Need to output to memory stream
                  var m = new MemoryStream();
                  dst.Save(m, format);
  
                  var img = Image.FromStream(m);
  
                  return img;
              }
          }
      }
  </code></pre>
  
  <p>As you can see I am using the suggested settings for Interpolation, Smoothing etc. I am also saving the jpeg with quality 100.</p>
  
  <p>The resultant image has noticeable blurring and artefacts even when resizing down to a modest 75% of its original size. I looked around and this is the recommended way. I found a simplistic way to resize and decided to give that a go.</p>
  
  <pre><code>Bitmap NewImg = new Bitmap(original, new Size(387,257));
  editor.SaveImage(@"C:\simpleResize.jpg", NewImg, ImageFormat.Jpeg);
  </code></pre>
  
  <p>Surprisingly this produces a much nicer image although it is quite a bit bigger ~30% larger in memory footprint.</p>
  
  <p>My question is, what's the difference and what setting am I missing on my resize routine that could account for the uglier result. Id like to get my resize routine to produce the exact same result as the simple resize.</p>
  
  <p>Your help is much appreciated. This is my first foray into image processing.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Simple Resize (82KB)</p>
  
  <p><a href="http://img189.imageshack.us/img189/2137/simpleresize.jpg" rel="nofollow noreferrer">simple http://img189.imageshack.us/img189/2137/simpleresize.jpg</a></p>
  
  <p>My Resize (55KB)</p>
  
  <p><a href="http://img12.imageshack.us/img12/4023/complexresize.jpg" rel="nofollow noreferrer">complex http://img12.imageshack.us/img12/4023/complexresize.jpg</a></p>