title: Rotating JPEGs in .NET with minimal loss of quality
body: <p>I am attempting to support rotating JPEG images from ASP.NET MVC (in 90 degree increments). I am attempting to use <code>System.Drawing</code> (GDI+), however I am running into issues.</p>
  
  <p>I tried using <a href="http://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip.aspx" rel="noreferrer"><code>Image.RotateFlip</code></a> which is able to rotate the image but causes a loss of quality. Even with an encoder quality of 100, there are still visible artifacts on the rotated image that weren't on the original image nor do they show up when I rotate it using other programs (Gimp, etc.).</p>
  
  <pre><code>using (Image image = Image.FromFile("C:\\source.jpg")) {
      ImageFormat sourceFormat = image.RawFormat;
      image.RotateFlip(RotateFlipType.Rotate90FlipNone);
      EncoderParameters encoderParams = null;
      try {
          if (sourceFormat == ImageFormat.Jpeg) {
              encoderParams = new EncoderParameters(1);
              encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
          }
          image.Save("C:\\target.jpg", GetEncoder(sourceFormat), encoderParams);
      } finally {
          if (encoderParams != null)
              encoderParams.Dispose();
      }
  }
  </code></pre>
  
  <p>I found an article on <a href="http://msdn.microsoft.com/en-us/library/ms533845(VS.85).aspx" rel="noreferrer">transforming a JPEG without loss of information</a>. Using <code>Encoder.Transformation</code> appears to be an option from .NET - however I cannot get it to cause any of my JPEG test images to rotate at all, whether or not the dimensions are a multiple of 16.</p>
  
  <pre><code>using (Image image = Image.FromFile("C:\\source.jpg")) {
      ImageFormat sourceFormat = image.RawFormat;
      EncoderParameters encoderParams = null;
      try {
          if (sourceFormat == ImageFormat.Jpeg) {
              encoderParams = new EncoderParameters(1);
              encoderParams.Param[0] = new EncoderParameter(Encoder.Transformation, 
                  (long)EncoderValue.TransformRotate90);
          }
          image.Save("C:\\target.jpg", GetEncoder(sourceFormat), encoderParams);
      } finally {
          if (encoderParams != null)
              encoderParams.Dispose();
      }
  }
  </code></pre>
  
  <p>Does anyone know how to successfully rotate a JPEG in .NET in 90 degree increments with minimal or no loss of quality using either of the above methods or another method? Thanks.</p>
  
  <p>Also, here's my implementation of <code>GetEncoder</code>:</p>
  
  <pre><code>private ImageCodecInfo GetEncoder(ImageFormat format) {
      foreach (var info in ImageCodecInfo.GetImageEncoders())
          if (info.FormatID == format.Guid)
              return info;
      return null;
  }
  </code></pre>
  
  <h1>Edit:</h1>
  
  <p>I updated the above code to better match my actual code. The bug was in the following line:</p>
  
  <pre><code>if (sourceFormat == ImageFormat.Jpeg) {
  </code></pre>
  
  <p>It should have been:</p>
  
  <pre><code>if (sourceFormat.Guid == ImageFormat.Jpeg.Guid) {
  </code></pre>