JpegImage AutoRotate throws ArgumentOutOfRange

When i use the AutoRotate() Method on a JpegImage the method throws an exception:

System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Specified argument was out of the range of valid values.
Source=Aspose.Imaging
StackTrace:
at Aspose.Imaging.FileFormats.Jpeg.JpegImage.AutoRotate()
at AutoRotateBug.Program.Main(String[] args) in D:\dev\Tryout\AutoRotateBug\AutoRotateBug\Program.cs:line 14

This works correctly on most jpg but not on the one attached.

Code to trigger the exception:
static void Main(string[] args)
{
var image = Aspose.Imaging.Image.Load(“49.jpg”);
if (image is Aspose.Imaging.FileFormats.Jpeg.JpegImage inputJpegImage)
{
if (inputJpegImage.JpegOptions?.ExifData?.Orientation != Aspose.Imaging.Exif.Enums.ExifOrientation.TopLeft)
{
inputJpegImage.AutoRotate();
}
}
}

49.jpg (2.6 MB)

The Aspose Imaging version is 22.5

Could you check if this is an known error?

Regards,
Mark Krommenhoek

(this look the same as JpegImage.autoRotate() throws Exception on some Jpeg images - #4 by mudassir.fayyaz but there is no solution in this topic)
ad. The forum software seems to adjust the attached jpg… I also have uploaded a zipfile with the image.49.zip (4.1 MB)

Hello,

Provided JPEG sample seems to have ExifOrientation value of 0. ExifOrientation enum values are enumerated from 1 to 8, so other than these values cause exceptions. As ExifOrientation.TopLeft is enumerated as 1 and it does not rotate image, here is a piece of code that might help you avoid exceptions:

using (var image = Image.Load("input.jpg"))
{
    if (image is JpegImage inputJpegImage)
    {
        var orientation = inputJpegImage.JpegOptions?.ExifData?.Orientation ?? 0;
        if (orientation > ExifOrientation.TopLeft && orientation <= 8)
        {
            inputJpegImage.AutoRotate();
        }
    }
}

The piece of code above check if orientation value is in range of 2 - 8, which corresponds to ExifOrientation enum values, which actually cause image rotation.