Rotate or Flip images in C# .NET

Aspose.Imaging’s Image Rotate Plugin for .NET provides an efficient and cost-effective method to rotate and flip images programmatically, allowing various rotation angles in conjunction with flipping. Designed to be an affordable solution, this plugin enables individual developers and freelancers to leverage Aspose’s renowned software for enhancing their application workflows without breaking the bank. Whether you need to rotate images by 90, 180, or 270 degrees, or simultaneously combine rotation with horizontal or vertical flipping, this plugin simplifies the process. In this article, you’ll learn how to rotate images in C# using Aspose.Imaging for .NET while exploring the best image rotation library for .NET.


Topics Covered:


C# API to Rotate Images

To rotate and flip images, we will leverage Aspose.Imaging for .NET. This comprehensive and powerful .NET image manipulation library supports various raster and vector image formats. You can easily integrate it into your project via NuGet or by downloading its DLL.

PM> Install-Package Aspose.Imaging

How to Rotate an Image in C#

To rotate an image, specify the rotation angle, commonly used angles include 90, 180, and 270 degrees. Additionally, you can rotate and flip an image simultaneously. Aspose.Imaging for .NET provides RotateFlipType enum to define both the rotation angle and flip type. The following image rotation types are available:

  • Rotate only
  • Both rotate and flip

The following steps outline how to rotate an image in C#:

  1. Load the image from disk.
  2. Specify the rotation type and rotate the image.
  3. Save the updated image.

The following is the image we will use to demonstrate image rotation:

Image for rotation and flipping

Rotate an Image in C#

Let’s explore how to rotate an image without flipping it. The following options allow rotation without any flipping:

  • Rotate180FlipNone: 180-degree rotation without flipping
  • Rotate270FlipNone: 270-degree rotation without flipping
  • Rotate90FlipNone: 90-degree rotation without flipping

To perform the rotation of an image in C#, follow these steps:

  1. Load the image file using the Image class.
  2. Rotate the image by 270 degrees using the Image.RotateFlip(RotateFlipType.Rotate270FlipNone) method.
  3. Save the updated image using the Image.Save(string) method.

Here’s a code snippet demonstrating how to rotate an image to a specific angle:

// Load the image file
using (var image = Aspose.Imaging.Image.Load("input.jpg"))
{
    // Rotate the image by 270 degrees
    image.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate270FlipNone);
    
    // Save the updated image
    image.Save("rotated.jpg");
}

This is the resulting rotated image after executing the code:

rotate an image in C#

Rotate an Image

C# Rotate an Image with Flipping

Next, let’s see how to rotate and flip images simultaneously—no additional code lines required. Simply utilize any of the following members from the 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

To rotate and flip an image in C#, use these steps:

  1. Load the image file using the Image class.
  2. Rotate the image 180 degrees while flipping it horizontally using the Image.RotateFlip(RotateFlipType.Rotate180FlipX) method.
  3. Finally, save the updated image using the Image.Save(string) method.

The following code snippet illustrates how to rotate and flip an image simultaneously:

// Load the image file
using (var image = Aspose.Imaging.Image.Load("input.jpg"))
{
    // Rotate the image by 180 degrees and flip horizontally
    image.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate180FlipX);
    
    // Save the updated image
    image.Save("rotated-flipped.jpg");
}

The image after applying the rotation and flipping looks as follows:

Rotate and flip an image in C#

Rotate and Flip an Image

C# Rotate Images with a Free License

You can get a free temporary license to rotate images without any evaluation limitations.

Conclusion

In this article, you learned how to rotate images programmatically in C#. You also explored the functionality to rotate and flip images simultaneously while maintaining the quality of the output. For more insights into the .NET image processing libraries, including a comparison of the available .NET image manipulation libraries, check out the documentation. You can also download the source code samples of the API from GitHub. If you have any queries, feel free to reach us at our forum.

See Also