views:

136

answers:

0

I'm performing rotation while resizing a JPEG image using GDI+.

If I use Image.RotateFlip(RotateFlipType) when saving a JPEG, it works great. When I pass in any of the Encoder.Transformation parameters, I get a "Parameter is not valid." ArgumentException.

I've read that using the Encoder.Transformation parameters during the save process will have lossless rotation if the image isn't being resized. So figured I'd try it in my resizing flow as well.

Here's a simplified overview of what I'm doing:

ImageCodecInfo encoder = this.GetImageEncoder(...);
using (Bitmap original = (Bitmap)Bitmap.FromFile(originalPath, true))
{
    EncoderParameters encoderParams = new EncoderParameters();
    encoderParams.Param = new EncoderParameter[]
    {
     new EncoderParameter(Encoder.Quality, 100L),
     new EncoderParameter(Encoder.Transformation, (long)EncoderValue.TransformRotate90)
    };

    using (Bitmap output = new Bitmap(width, height))
    {
     using (Graphics g = Graphics.FromImage(output))
     {
      g.DrawImage(...);
     }
     output.Save(outputPath, encoder, encoderParams);
    }
}

If I change the last line to save from the original Bitmap with the same exact parameters it works great. So I'm wondering, what are the restrictions when rotating via encoding parameters?

As a follow up question, what does it do if I pass in multiple Encoder.Transformation? Does it just perform them in order?