While working with images, you may often need to rotate them at different angles. Moreover, horizontal and vertical flips are also used in various cases. Both are considered the key features of every image editor. In this article, you will learn how to rotate and flip images programmatically in C#.
C# .NET API to Rotate and Flip Images
To rotate and flip images from within .NET applications, we will use Aspose.Imaging for .NET. It is an image processing API that provides a wide range of features to manipulate various types of images. Furthermore, it hides the complex operations and makes it quite easier for you to perform image processing. You can download the API or install it into your .NET applications using NuGet.
PM> Install-Package Aspose.Imaging
Rotate and Flip an Image in C#
To rotate an image, you need to specify a rotation angle. The most commonly used angles are 90, 180, and 270 degrees. In addition to that, the images are flipped horizontally or vertically. Aspose.Imaging for .NET takes care of rotation as well as flipping at the same time. For this, the API provides RotateFlipType enum to specify the rotation angle and flip type. You can perform the following flip/rotate operations on an image:
- Rotate only
- Flip only
- Both rotate and flip
Let’s see how to perform each of the above-mentioned operations on the following image.
Rotate an Image in C#
First, let’s have a look at how to rotate an image. The following options are used to rotate an image without flipping.
- Rotate180FlipNone: 180-degree rotation without flipping
- Rotate270FlipNone: 270-degree rotation without flipping
- Rotate90FlipNone: 90-degree rotation without flipping
The following are the steps to perform the rotation of an image in C#.
- First, load the image file using Image class.
- Then, rotate image to 270 degrees using Image.RotateFlip(RotateFlipType.Rotate270FlipNone) method.
- Finally, save updated image using Image.Save(string) method.
The following code snippet shows how to rotate an image to a particular angle.
The following is the rotated image that we get after executing this code.
Flip an Image in C#
Now, take a look at how to flip an image without rotation. For this, the following RotateFlipType members are used.
- RotateNoneFlipX: No rotation with horizontal flipping
- RotateNoneFlipY: No rotation with vertical flipping
- RotateNoneFlipXY: No rotation with horizontal and vertical flipping
The following are the steps to flip an image in C#.
- First, load the image file using Image class.
- Then, flip the image horizontally using Image.RotateFlip(RotateFlipType.RotateNoneFlipX) method.
- Finally, save updated image using Image.Save(string) method.
The following code sample shows how to flip an image in .NET applications.
The following screenshot shows how the image is flipped using the code snippet above.
Rotate and Flip an Image in C#
In this section, we will see how to rotate and flip the image at the same time. To achieve this, the following members of RotateFlipType enum are used.
- Rotate180FlipX: 180-degree rotation with horizontal flipping
- Rotate180FlipXY: 180-degree rotation with horizontal and vertical flipping
- Rotate180FlipY: 180-degree rotation with vertical flipping
- Rotate270FlipX: 270-degree rotation with horizontal flipping
- Rotate270FlipXY: 270-degree rotation with horizontal and vertical flipping
- Rotate270FlipY: 270-degree rotation with vertical flipping
- Rotate90FlipX: 90-degree rotation with horizontal flipping
- Rotate90FlipXY: 90-degree rotation with horizontal and vertical flipping
- Rotate90FlipY: 90-degree rotation with vertical flipping
The following are the steps to rotate and flip an image in C#.
- First, load the image file using Image class.
- Then, rotate the image on 180 degree and flip it horizontally using Image.RotateFlip(RotateFlipType.Rotate180FlipX) method.
- Finally, save updated image using Image.Save(string) method.
The following C# code snippet shows how to perform rotation and flipping at the same time.
The following screenshot shows how the image looks like after we apply rotation and flipping.
Get a Free API License
You can get a free temporary license to use Aspose.Imaging for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to rotate and flip images programmatically in C#. Furthermore, the step by step guide and code samples have demonstrated how to rotate an image, flip an image, and rotate and flip an image at the same time. Besides, you can explore more about .NET image processing API using documentation. Also, you can download the source code samples of the API from GitHub. In case of any queries, you can reach us at our forum.