How to rotate an image
How to rotate an image
Rotate and flip an image
The first type of rotating image is a rotation to a fixed angle of 90/180/270-degree with or without a flip. You just need to load your image and apply the RotateFlip method with a rotate type parameter. We use the type `Rotate270FlipNone` in this example:
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Loading and Rotating Image | |
using (var image = Image.Load(dataDir + "template.jpg")) | |
{ | |
image.RotateFlip(RotateFlipType.Rotate270FlipNone); | |
image.Save(dataDir + "result.jpg"); | |
} | |
File.Delete(dataDir + "result.jpg"); |
For a full list of available types please see the link to the rotate and flip types parameters descriptions.
Rotate an image to the arbitrary angle
The second type is rotating for an arbitrary choice angle. The rotation angle value could be positive if a rotation is clockwise or negative for the opposite direction. In this case, the size of the original image could be changed after rotating the image, so you have to specify `Resize proportionally` boolean parameter to indicate should the resulting image resized proportionally or not. Additionally, you could specify the background color to fill empty corners of the rotated image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image to be rotated in an instance of RasterImage | |
using (RasterImage image = (RasterImage)Image.Load(dataDir + "template.jpg")) | |
{ | |
// Before rotation, the image should be cached for better performance | |
if (!image.IsCached) | |
{ | |
image.CacheData(); | |
} | |
// Perform the rotation on 20 degree while keeping the image size proportional with red background color and Save the result to a new file | |
image.Rotate(20f, true, Color.Red); | |
image.Save(dataDir + "result.jpg"); | |
} | |
File.Delete(dataDir + "result.jpg"); |