Rotating and Flipping Images

Image rotating and flipping are basic imaging operations. As you know, Graphics Mill provides two ways to perform geometrical raster transformations: in-place and out-of-place. This article describes both approaches to performing rotating and flipping and provides code snippets.

Let us examine both operations in more detail.

Rotating an Image

In Graphics Mill positive degree numbers cause a clockwise rotation (demonstrated on the picture below) and negative degree numbers cause a counter-clockwise rotation.

clockwise rotation (positive degree number)

A special case is rotation by 90, 180, or 270 degrees, because it uses a special algorithm which avoids floating-point calculations yielding to higher performance. Also in this case the rotated image remains rectangular, thus you get the rotated version of a source image without additional background. In other cases the rotated image is not rectangular, so it will be padded with the background color to get a rectangular image.

Quality and speed of the rotation depends on the interpolation mode used. Better quality means lower speed. The InterpolationMode enumeration exposes the available interpolation modes, including High, Medium, and Low. To get more information about interpolation modes read the Interpolation Modes Comparison topic.

You can specify the background color and the interpolation mode as arguments of the TransformsProvider.Rotate method if you use the Bitmap.Transforms property to perform rotation. If you prefer to utilize the Transforms.Rotate class, then you can set the background color and the interpolation mode as constructor arguments or through the Rotate.BackgroundColor and Rotate.InterpolationMode properties.

If you do not specify background color and interpolation mode, the transparent white color and the high quality interpolation mode are used.

The following code rotates an image by 5 degrees clockwise using the Bitmap.Transforms property:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
{
    bitmap.Transforms.Rotate(5, RgbColor.Yellow, InterpolationMode.Medium);
    bitmap.Save(@"Images\Output\out.jpg");
}

The following code rotates an image by 5 degrees clockwise using the Transforms.Rotate class:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var rotate = new Rotate(5, RgbColor.Yellow, InterpolationMode.Medium))
{
    using (var result = rotate.Apply(bitmap))
    {
        result.Save(@"Images\Output\out.jpg");
    }
}

Flipping an Image

To flip an image you can use the Transforms.Flip class or the Bitmap.Transforms property. In both cases you have to specify the flip mode. The Transforms.FlipType class exposes the available flip modes: Horizontal, Vertical, and Both.

The following code flips an image horizontally using the Bitmap.Transforms property:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
{
    bitmap.Transforms.Flip(FlipType.Horizontal);
    bitmap.Save(@"Images\Output\out.jpg");
}

The following code flips an image horizontally using the Transforms.Flip class:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var flip = new Flip(FlipType.Horizontal))
{
    using (var result = flip.Apply(bitmap))
    {
        result.Save(@"Images\Output\out.jpg");
    }
}

See Also

Reference

Manual