---
title: "Convert an image to WebP in C#"
url: https://recursiveai.org/oneimage/cookbook/convert-to-webp/
description: "Convert JPG, PNG or GIF to WebP in C# with one line of pure .NET code, no native libwebp: quality, lossless and animated WebP. Runnable code with OneImage."
version: "1.10.4"
---

# Convert an image to WebP in C#

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.

```shell
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`:

```csharp
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:

```csharp
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](https://recursiveai.org/oneimage/cookbook/resize-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](https://caniuse.com/webp)).

To shrink files further, see [Compress an image](https://recursiveai.org/oneimage/cookbook/compress-image/). The WebP encoder’s options are in the [guide](https://recursiveai.org/oneimage/docs/guide/#saving), and its speed against ImageSharp and the native wrappers in the [benchmarks](https://recursiveai.org/oneimage/docs/performance/).
