Recursive AI

Rotate or flip an image in C#

All oneimage recipes

Load the image, call Rotate with an angle in degrees, clockwise, or Flip with an axis, and save. Multiples of 90° are exact pixel moves; other angles grow the canvas to fit the turned picture. The program below uses a landscape crop of NASA’s “Blue Marble” photograph (public domain).

Terminal
dotnet new console -o RotateImage && cd RotateImage
dotnet add package RecursiveAI.OneImage --version 1.10.4
curl -L -o photo.jpg \
  https://images-assets.nasa.gov/image/as17-148-22727/as17-148-22727~orig.jpg

OneImage needs the .NET 10 SDK. Put this in Program.cs and run dotnet run:

C#
using OneImage;
 
// A 1000x600 landscape crop, so the turns show.
Image photo = Image.Load("photo.jpg").Resize(width: 1000, height: 600, fit: Fit.Cover);
Console.WriteLine($"photo: {photo.Width}x{photo.Height}");
 
Image quarter = photo.Rotate(90);                   // clockwise
Image upsideDown = photo.Rotate(180);
Image mirrored = photo.Flip(FlipAxis.Horizontal);   // left to right
Image tilted = photo.Rotate(15, background: new Rgba32(255, 255, 255, 255));
 
quarter.Save("rotate-90.jpg");
upsideDown.Save("rotate-180.jpg");
mirrored.Save("flip.jpg");
tilted.Save("rotate-15.jpg");
 
Console.WriteLine($"rotate-90.jpg: {quarter.Width}x{quarter.Height}");
Console.WriteLine($"rotate-180.jpg: {upsideDown.Width}x{upsideDown.Height}");
Console.WriteLine($"flip.jpg: {mirrored.Width}x{mirrored.Height}");
Console.WriteLine($"rotate-15.jpg: {tilted.Width}x{tilted.Height}");
Text
photo: 1000x600
rotate-90.jpg: 600x1000
rotate-180.jpg: 1000x600
flip.jpg: 1000x600
rotate-15.jpg: 1122x839

A 90° turn swaps width and height. The 15° turn grows the canvas to the turned picture’s bounding box and fills the corners with background: (transparent by default, which JPEG can’t store, hence white here). Use -90 or 270 to turn counterclockwise, and FlipAxis.Vertical to flip top to bottom.

Phone photos that come out sideways

If a photo looks right in a viewer but sideways after your code saves it, don’t rotate by hand: its EXIF orientation tag wasn’t applied. OneImage applies it when it loads, so a plain Load and Save already writes it upright. See Fix EXIF orientation.

Limits

  • JPEG is re-encoded. Even a 90° turn decodes and encodes the JPEG again, so it isn’t lossless. Save as PNG to keep the pixels exactly.
  • Arbitrary angles resample. Edges are smooth but slightly soft; pass expand: false to keep the original canvas size and crop the corners.
  • Animations turn every frame.

Affine and Perspective cover skews and keystone correction; see the guide.