How to Rotate an Image by 90, 180, or 270 Degrees in .NET
Rotating images by common angles is essential for photo management, scanning, and design workflows. Aspose.Imaging for .NET lets you rotate by 90, 180, or 270 degrees in just a few lines of C# code.
Real-World Problem
Photos from scanners, cameras, or user uploads are often upside down or sideways. Manual correction is slow—automation saves time and prevents mistakes.
Solution Overview
Use Aspose.Imaging’s RotateFlip method to quickly rotate any image file to the correct orientation, with full support for batch or single-file jobs.
Prerequisites
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.Imaging for .NET from NuGet
- Image file (JPEG, PNG, BMP, etc.)
PM> Install-Package Aspose.ImagingStep-by-Step Implementation
Step 1: Load and Rotate Your Image
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
string inputPath = "./input/photo.jpg";
string output90 = "./output/photo_rotated_90.jpg";
string output180 = "./output/photo_rotated_180.jpg";
string output270 = "./output/photo_rotated_270.jpg";
using (var image = Image.Load(inputPath))
{
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
image.Save(output90, new JpegOptions { Quality = 90 });
}
using (var image = Image.Load(inputPath))
{
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.Save(output180, new JpegOptions { Quality = 90 });
}
using (var image = Image.Load(inputPath))
{
image.RotateFlip(RotateFlipType.Rotate270FlipNone);
image.Save(output270, new JpegOptions { Quality = 90 });
}Step 2: Review the Results
Open the outputs to confirm the correct rotation. Use the option that best fits your workflow.
Use Cases and Applications
- Auto-correcting photos from scanners and phones
- Prepping documents for web, print, or archive
- Batch-rotating images in compliance or record workflows
Common Challenges and Solutions
Wrong orientation: Try each angle and check preview. Use batch automation for large sets.
Loss of quality in repeated edits: Save once, or use lossless formats for editing.
Need to rotate and resize/crop: Combine RotateFlip with other Aspose.Imaging methods.
Best Practices
- Archive originals before processing
- Document the correct angle for each image source
- Use batch scripts for high-volume jobs
FAQ
Q: Can I rotate PNG, TIFF, or BMP images? A: Yes—Aspose.Imaging supports all major raster formats.
Q: Can I rotate all images in a folder? A: Yes—wrap this code in a loop and apply to every file.
Q: Can I rotate and flip at the same time? A: Yes—see next articles for flip and combined rotation+flip examples.
Conclusion
Rotating images is quick and easy with Aspose.Imaging for .NET. For more on batch automation or combining with other edits, see the Aspose.Imaging for .NET API Reference .