Rotate or Flip images in C# .NET

Aspose.Imaging’s Image Rotate Plugin for .NET offers a highly efficient and budget-friendly solution for programmatically rotating and flipping images. This plugin allows for various rotation angles and the ability to combine rotation with flipping, making it an ideal choice for individual developers and freelancers. With Aspose’s reputation for excellence, you can enhance your application workflows without exceeding your budget. Whether you need to rotate images by 90, 180, or 270 degrees, or wish to apply horizontal or vertical flipping, this plugin streamlines the process. In this article, you will discover how to rotate images in C# using Aspose.Imaging for .NET and learn about the best image rotation library available for .NET.


Topics Covered:


C# API to Rotate Images

To rotate and flip images, we will utilize Aspose.Imaging for .NET. This powerful .NET image manipulation library supports a wide range of 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 the 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 steps to rotate an image in C# are outlined below:

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

Here’s 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#, follow 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 obtain a free temporary license to rotate images without any evaluation limitations.

Conclusion

In this article, you learned how to programmatically rotate images in C#. You also explored the functionality to rotate and flip images simultaneously while maintaining the quality of the output. For more insights into .NET image processing libraries, including a comparison of 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