Image processing and editing is commonly used in various fields these days. As a programmer, you may also come across the scenario where you need to implement image rotation feature. In this article, you will learn how to rotate an image in C#. We will also cover how to rotate the images at different angles programmatically.
- C# API to Rotate Images - Free Download
- Steps to Rotate an Image in C#
- Rotate an Image in C#
- Rotate and Flip an Image in C#
Rotate Images in C# - API Installation
Aspose.Imaging for .NET is a powerful and feature-rich image processing API. It lets you manipulate the popular image types and implement image editing features seamlessly. To rotate the images, we will use this API. You can download the API or install it into your .NET applications using NuGet.
PM> Install-Package Aspose.Imaging
How to Rotate 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, you can perform rotation and flipping of an image at the same time. Aspose.Imaging for .NET provides RotateFlipType enum to specify the rotation angle and flip type. The following image rotation types are used to rotate an image:
- Rotate only
- Both rotate and flip
The following are the steps to rotate an image in C#.
- Load the image from disk.
- Specify the rotation type and rotate the image.
- Save the updated image.
The following is the image that we will use to demonstrate image rotation.
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.
C# Rotate an Image with Flipping
Let’s now have a look at how to rotate and flip images at the same time. This doesn’t require any additional efforts or lines of code. Simply use any of the following members of RotateFlipType enum.
- 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 code snippet shows how to rotate and flip image at the same time.
The following is the screenshot of the image after applying rotation and flipping at the same time.
C# Rotate Images with a Free License
You can get a free temporary license to rotate images without evaluation limitations.
Conclusion
In this article, you have learned how to rotate images programmatically in C#. Furthermore, you have seen how to rotate and flip images 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.