Recursive AI

Convert an image to WebP in C#

All oneimage recipes

Load the image and save it with a .webp extension. That writes lossy WebP at quality 75; set Quality or Lossless in WebPEncoderOptions to change it. OneImage’s WebP encoder is pure C#, so there is no native libwebp to install. The program below converts NASA’s “Blue Marble” photograph (public domain) and a transparent PNG logo.

Terminal
dotnet new console -o ToWebP && cd ToWebP
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
curl -L -o logo.png https://recursiveai.org/assets/brand/icon-512.png

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

C#
using OneImage;
 
// JPG, PNG, GIF, TIFF, BMP or ICO in; lossy WebP at quality 75 out.
Image.Load("photo.jpg").Save("photo.webp");
 
// Choose the quality (0 to 100) and the effort (0 fast to 6 smallest).
Image.Load("photo.jpg").Save("photo-q60.webp", new SaveOptions
{
    EncoderOptions = new WebPEncoderOptions { Quality = 60, Effort = 6 },
});
 
// Lossless for logos, screenshots and line art; transparency is kept.
Image.Load("logo.png").Save("logo.webp", new SaveOptions
{
    EncoderOptions = new WebPEncoderOptions { Lossless = true },
});
 
foreach (string file in new[] { "photo.jpg", "photo.webp", "photo-q60.webp",
                                "logo.png", "logo.webp" })
{
    ImageInfo info = Image.Identify(file);
    long kb = new FileInfo(file).Length / 1024;
    string size = $"{info.Width}x{info.Height}";
    Console.WriteLine($"{file}: {info.Format.Name} {size}, {kb} KB");
}
Text
photo.jpg: jpeg 4579x4579, 1870 KB
photo.webp: webp 4579x4579, 676 KB
photo-q60.webp: webp 4579x4579, 540 KB
logo.png: png 512x512, 7 KB
logo.webp: webp 512x512, 4 KB

The full-size photo shrinks to about a third of its JPEG size at the same dimensions, and the lossless logo keeps every pixel, alpha included. Lossy WebP keeps transparency too: its alpha channel is stored losslessly. The other way round, Image.Load("photo.webp").Save("photo.png") converts WebP to PNG or JPG.

Resize and convert for the web

A WebP for a web page should also be the displayed size, upright and free of private metadata. Image.Thumbnail does all of that:

C#
using OneImage;
 
// At most 1200 px wide, upright, sRGB, EXIF and GPS removed.
Image.Thumbnail("photo.jpg", 1200).Save("photo-1200.webp");
 
ImageInfo info = Image.Identify("photo-1200.webp");
long kb = new FileInfo("photo-1200.webp").Length / 1024;
Console.WriteLine($"photo-1200.webp: {info.Width}x{info.Height}, {kb} KB");
Text
photo-1200.webp: 1200x1200, 114 KB

Animated GIF to animated WebP

Saving an animation to .webp writes every frame with its delay and the loop count: Image.Load("in.gif").Save("out.webp"). Animated WebP is usually much smaller than GIF; Resize an animated GIF has numbers.

Limits

  • Quality numbers differ between formats. WebP at 75 and JPEG at 75 aren’t the same image; compare by eye at the size you display.
  • Flat-color animations can grow. A small GIF with few colors may get larger as lossy WebP; try Lossless = true and keep whichever is smaller.
  • Metadata is kept by a plain load and save: the ICC profile and EXIF travel into the WebP. Use Thumbnail or StripMetadata() for public files.
  • Browsers: current Chrome, Edge, Firefox and Safari display WebP (Can I use).

To shrink files further, see Compress an image. The WebP encoder’s options are in the guide, and its speed against ImageSharp and the native wrappers in the benchmarks.