Load the image, call Resize with the width you want, and save it. The height follows the aspect ratio. The program below resizes NASA’s “Blue Marble” photograph (4579×4579, public domain) three ways. OneImage is pure C#, so the same code runs on Windows, Linux, macOS and in containers.
dotnet new console -o ResizeImage && cd ResizeImage 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:
using OneImage; Image photo = Image.Load("photo.jpg"); // reads the headers only // 800 px wide; the height follows the aspect ratio. photo.Resize(width: 800).Save("photo-800.jpg"); // Fill an exact 800x450 box and crop what overflows, keeping the center. photo.Resize(width: 800, height: 450, fit: Fit.Cover).Save("banner.jpg"); // Fit inside 800x450 and pad the rest with white. photo.Resize(width: 800, height: 450, fit: Fit.Pad, background: new Rgba32(255, 255, 255, 255)) .Save("padded.jpg"); foreach (string file in new[] { "photo-800.jpg", "banner.jpg", "padded.jpg" }) { ImageInfo info = Image.Identify(file); long kb = new FileInfo(file).Length / 1024; Console.WriteLine($"{file}: {info.Width}x{info.Height}, {kb} KB"); }
photo-800.jpg: 800x800, 90 KB banner.jpg: 800x450, 84 KB padded.jpg: 800x450, 43 KB
The output format follows the file extension, so .png, .webp, .gif, .tif, .bmp and .ico work the same way. Image is immutable, so one loaded photo feeds all three. Nothing is decoded until Save, and a large JPEG is decoded at 1/2, 1/4 or 1/8 scale when that is enough for the target, so the full-size photo is never held in memory.
Fit modes
Give both a width and a height, and fit: decides how the image fills the box:
Fit | Result |
|---|---|
Contain (default) | Fits inside the box, aspect ratio kept; one side may be smaller. |
Cover | Fills the box, aspect ratio kept; the overflow is cropped. anchor: picks the part kept (nine positions, or Anchor.Smart for the busiest area). |
Pad | Fits inside the box, then pads to the exact size with background:. |
BoxPad | Like Pad, but never enlarges. |
Min | Covers the box without cropping; never enlarges. |
Stretch | Exactly the box; aspect ratio not kept. |
Quality and several sizes at once
The default filter already uses Lanczos3 when shrinking and Catmull-Rom when enlarging, in linear light, so the setting that matters most is the encoder’s quality. This writes JPEG at quality 85 and the three widths a srcset needs, as WebP:
using OneImage; Image photo = Image.Load("photo.jpg"); var jpeg = new SaveOptions { EncoderOptions = new JpegEncoderOptions { Quality = 85 }, }; photo.Resize(width: 1200).Save("photo-1200.jpg", jpeg); foreach (int width in new[] { 320, 640, 1280 }) { // upscale: false treats the width as a maximum for smaller sources. photo.Resize(width: width, upscale: false).Save($"photo-{width}.webp"); } foreach (string file in new[] { "photo-1200.jpg", "photo-320.webp", "photo-640.webp", "photo-1280.webp" }) { Console.WriteLine($"{file}: {new FileInfo(file).Length / 1024} KB"); }
photo-1200.jpg: 216 KB photo-320.webp: 24 KB photo-640.webp: 53 KB photo-1280.webp: 140 KB
JPEG and WebP default to quality 75. filter: ResamplingFilter.Nearest keeps hard pixel edges for pixel art; the other filters are listed in the guide.
Limits
Resizeenlarges when asked to. Passupscale: falsewhen the width is a maximum, or useImage.Thumbnail, which never enlarges.- Metadata is kept. A plain load and save keeps EXIF, which can hold the GPS position of a phone photo. For anything public, use
Thumbnailor remove the metadata. - Formats. The core package reads and writes PNG, JPEG, WebP, GIF, TIFF, BMP and ICO. SVG, PSD and PDF come in add-on packages; HEIC and AVIF aren’t supported.
- .NET 10 only. On older .NET,
System.Drawingis Windows-only since .NET 6: see the “not supported on this platform” error.
For web copies, see Create a thumbnail; to cut a region at full resolution, Crop an image. Every option is in the guide.