---
title: "Autorotate"
canonical: https://www.graphicsmill.com/samples/Transforms/RotateAndFlip/Autorotate_cs
product: "Graphics Mill"
source: "Transforms/RotateAndFlip/Autorotate.cs"
---

# Autorotate

Automatically rotates image based on EXIF orientation metadata.

## Instruction

Write a C# program that automatically rotates an image "Gondola.jpg" based on EXIF orientation metadata using ImageReader to extract orientation value, then applies appropriate Rotate and Flip transforms via Pipeline API for non-JPEG formats or LosslessJpeg for JPEG files to preserve quality, updates metadata to reset orientation to 1, and saves as "Autorotate.jpg".

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
```

## Code Snippet

```csharp
public static void Run()
{
    Autorotate("Gondola.jpg", "Autorotate.jpg");
}

private static bool Autorotate(string pathIn, string pathOut)
{
    ushort orientation;

    using (var reader = ImageReader.Create(pathIn))
    {
        var exif = reader.Exif;

        if (exif == null || !exif.Contains(ExifDictionary.Orientation))
        {
            return false;
        }

        orientation = (ushort)exif[ExifDictionary.Orientation];

        // 1         2       3      4         5            6           7          8

        // ######  ######      ##  ##      ##########  ##                  ##  ##########
        // ##          ##      ##  ##      ##  ##      ##  ##          ##  ##      ##  ##
        // ####      ####    ####  ####    ##          ##########  ##########          ##
        // ##          ##      ##  ##
        // ##          ##  ######  ######

        if (orientation == 1)
        {
            return false;
        }

        using (var writer = ImageWriter.Create(pathOut))
        {
            if (!(reader is JpegReader) || !(writer is JpegWriter))
            {
                RotateWithRecompression(reader, writer, orientation);

                return true;
            }
        }
    }

    RotateJpegWithRecompression(pathIn, pathOut, orientation);

    return true;
}

private static void RotateWithRecompression(ImageReader reader, ImageWriter writer, uint orientation)
{
    using (var rotate = new Aurigma.GraphicsMill.Transforms.Rotate())
    using (var flip = new Aurigma.GraphicsMill.Transforms.Flip())
    {
        flip.Type = Aurigma.GraphicsMill.Transforms.FlipType.None;

        switch (orientation)
        {
            case 2:
                flip.Type = Aurigma.GraphicsMill.Transforms.FlipType.Vertical;
                break;

            case 3:
                rotate.Angle = 180f;
                break;

            case 4:
                flip.Type = Aurigma.GraphicsMill.Transforms.FlipType.Vertical;
                rotate.Angle = 180f;
                break;

            case 5:
                flip.Type = Aurigma.GraphicsMill.Transforms.FlipType.Horizontal;
                rotate.Angle = 270f;
                break;

            case 6:
                rotate.Angle = 90f;
                break;

            case 7:
                flip.Type = Aurigma.GraphicsMill.Transforms.FlipType.Horizontal;
                rotate.Angle = 90f;
                break;

            case 8:
                rotate.Angle = 270;
                break;
        }

        var jpegWriter = writer as JpegWriter;

        if (jpegWriter != null)
        {
            jpegWriter.Quality = 85;
        }

        var metadataWriter = writer as IMetadataWriter;

        if (metadataWriter != null)
        {
            metadataWriter.Exif = reader.Exif ?? new ExifDictionary();
            metadataWriter.Exif[ExifDictionary.Orientation] = 1;

            metadataWriter.Iptc = reader.Iptc;
            metadataWriter.AdobeResources = reader.AdobeResources;
            metadataWriter.Xmp = reader.Xmp;
        }

        if (flip.Type == Aurigma.GraphicsMill.Transforms.FlipType.None)
        {
            Aurigma.GraphicsMill.Pipeline.Run(reader + rotate + writer);
        }
        else if (rotate.Angle == 0)
        {
            Aurigma.GraphicsMill.Pipeline.Run(reader + flip + writer);
        }
        else
        {
            Aurigma.GraphicsMill.Pipeline.Run(reader + rotate + flip + writer);
        }
    }
}

private static void RotateJpegWithRecompression(string pathIn, string pathOut, uint orientation)
{
    using (var losslessJpeg = new LosslessJpeg(pathIn))
    {
        System.Drawing.RotateFlipType flipType;

        switch (orientation)
        {
            case 2:
                flipType = System.Drawing.RotateFlipType.RotateNoneFlipX;
                break;

            case 3:
                flipType = System.Drawing.RotateFlipType.Rotate180FlipNone;
                break;

            case 4:
                flipType = System.Drawing.RotateFlipType.Rotate180FlipX;
                break;

            case 5:
                flipType = System.Drawing.RotateFlipType.Rotate90FlipX;
                break;

            case 6:
                flipType = System.Drawing.RotateFlipType.Rotate90FlipNone;
                break;

            case 7:
                flipType = System.Drawing.RotateFlipType.Rotate270FlipX;
                break;

            case 8:
                flipType = System.Drawing.RotateFlipType.Rotate270FlipNone;
                break;

            default:
                flipType = System.Drawing.RotateFlipType.RotateNoneFlipNone;
                break;
        }

        losslessJpeg.Exif[ExifDictionary.Orientation] = 1;

        losslessJpeg.WriteRotated(pathOut, flipType);
    }
}
```

## Explanation

Below is a C# code sample that implements automatic image rotation based on EXIF orientation:

The code demonstrates comprehensive EXIF-aware autorotation handling both JPEG (using LosslessJpeg for quality preservation via System.Drawing.RotateFlipType) and non-JPEG formats (using Rotate/Flip transforms in Pipeline). It extracts orientation from ExifDictionary, maps EXIF values 2-8 to rotation angles (90/180/270) and flip types (horizontal/vertical), preserves metadata (EXIF/IPTC/Adobe/XMP) while resetting orientation to 1, and uses JPEG quality 85 for recompressed formats. The dual-path approach optimizes quality and performance based on input codec type.

## Tags

- [Bitmap](<https://www.graphicsmill.com/samples/tags/bitmap_md>): Working with images via the Bitmap API
- [EXIF](<https://www.graphicsmill.com/samples/tags/exif_md>): Managing EXIF metadata
- [Rotate](<https://www.graphicsmill.com/samples/tags/rotate_md>): Rotating images
- [Transform](<https://www.graphicsmill.com/samples/tags/transform_md>): Image transforms and effects

## Input

- [Gondola.jpg](<https://www.graphicsmill.com/samples/Transforms/compiled/Debug/Gondola_jpg>) (preview)

## Output

- [Autorotate.jpg](<https://www.graphicsmill.com/samples/Transforms/compiled/Debug/Autorotate_jpg>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ExifDictionary](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ExifDictionary.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.AdobeResources](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_AdobeResources.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageReader_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Exif](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_Exif.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Iptc](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_Iptc.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Xmp](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_Xmp.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageWriter.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageWriter_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IMetadataWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_IMetadataWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IMetadataWriter.AdobeResources](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_IMetadataWriter_AdobeResources.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IMetadataWriter.Exif](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_IMetadataWriter_Exif.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IMetadataWriter.Iptc](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_IMetadataWriter_Iptc.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IMetadataWriter.Xmp](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_IMetadataWriter_Xmp.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegWriter.Quality](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_JpegWriter_Quality.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_LosslessJpeg.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg.Exif](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_LosslessJpeg_Exif.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg.WriteRotated(String,RotateFlipType)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_LosslessJpeg_WriteRotated_2_ed07628a.htm.md>)
- [Aurigma.GraphicsMill.Codecs.MetadataDictionary.Contains(Object)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_MetadataDictionary_Contains_1_d02429b3.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Flip](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Flip.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Flip.Type](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Flip_Type.htm.md>)
- [Aurigma.GraphicsMill.Transforms.FlipType](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_FlipType.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Rotate](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Rotate.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Rotate.Angle](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Rotate_Angle.htm.md>)
