---
title: "Save a lossless WebP in C#"
url: https://recursiveai.org/oneimage/cookbook/write-lossless-webp/
description: "Save a lossless WebP in C# with OneImage. Set Lossless to true and run a PNG-to-WebP example that checks every decoded RGBA value, including transparency."
version: "1.11.2"
---

# Save a lossless WebP in C#

**OneImage** is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/write-lossless-webp/write-lossless-webp.zip)

Save 8-bit pixels unchanged. Use `swatches.png` from the download.

```shell
dotnet add package RecursiveAI.OneImage
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneImage;
var encoder = new WebPEncoderOptions
    { Lossless = true };
Image.Load("swatches.png").Save("lossless.webp",
    new() { EncoderOptions = encoder });
Console.WriteLine("Wrote lossless.webp");
```

Unzip [the example](https://recursiveai.org/oneimage/cookbook/write-lossless-webp/write-lossless-webp.zip), then `dotnet run`.

```text
Wrote lossless.webp
```

`Quality = 100` alone still gives lossy WebP.

`swatches.png` is a generated 16×16 PNG made of four 8×8 squares. The top row is opaque blue and half-transparent orange; the bottom row is opaque green and fully transparent purple. The purple pixels have RGBA values `(160, 40, 200, 0)`, even though you can’t see their color.

## The full program

```csharp
using OneImage;

Image image = Image.Load("swatches.png");
image.Save("lossless.webp", new SaveOptions
{
    EncoderOptions = new WebPEncoderOptions
    {
        Lossless = true
    }
});
Console.WriteLine("Wrote lossless.webp");

Rgba32[] before = image.ToArray<Rgba32>();
Rgba32[] after = Image.Load("lossless.webp").ToArray<Rgba32>();
Console.WriteLine($"RGBA pixels match: {before.SequenceEqual(after)}");
```

`dotnet run` prints:

```text
Wrote lossless.webp
RGBA pixels match: True
```

The program reloads the WebP and compares its decoded pixels to the PNG’s, including RGB under full transparency. A corrupt input can throw `ImageFormatException` when OneImage reads the pixels to save the file.

## Variations

**Keep lossless encoding, but change the search effort.** `Effort` defaults to 4 and accepts 0–6. Higher effort spends more time looking for a smaller file without changing the pixels.

```csharp
using OneImage;
var webp = new WebPEncoderOptions
{
    Lossless = true,
    Effort = 6
};
Image.Load("swatches.png").Save("effort-6.webp",
    new SaveOptions { EncoderOptions = webp });
```

## Related recipes

- [Convert an image to WebP](https://recursiveai.org/oneimage/cookbook/convert-to-webp/)
- [Set the PNG compression level](https://recursiveai.org/oneimage/cookbook/set-png-compression-level/)

---

## The short answer

Pass `new WebPEncoderOptions { Lossless = true }` as `SaveOptions.EncoderOptions` and save to a `.webp` path. The extension selects the format; the option selects its lossless mode. OneImage ignores `Quality` when `Lossless` is on.

## Does WebP support lossless compression?

Yes. [The WebP container specification](https://developers.google.com/speed/webp/docs/riff_container) defines both lossy `VP8` and lossless `VP8L` image data, so the file extension alone doesn’t tell you which one you have. In OneImage, an ordinary `Save("image.webp")` uses lossy encoding at quality 75. Choose `Lossless = true` explicitly for graphics such as icons or screenshots where pixel values matter.

Here, lossless describes how the encoder stores the pixels it receives, not whether the new file copies the original file’s bytes. If you start with a JPEG, the WebP stores the pixels decoded from that JPEG; it doesn’t restore information lost when the JPEG was made. OneImage’s WebP encoder accepts 8-bit RGB and RGBA. Don’t use the sample’s 8-bit comparison to check that a 16-bit source kept its original channel values.

## Does lossless WebP preserve transparency?

OneImage’s lossless mode preserves alpha, the channel that measures how opaque each pixel is. It also defaults to `Exact = true`, which keeps the red, green, and blue values of pixels whose alpha is zero. That’s why the invisible purple in the sample can be compared just like the visible blue. If only the rendered image matters, setting `Exact = false` lets the encoder replace those hidden colors for compression. Leave it at its default when another tool needs to read the same values back.

When checking a conversion, compare decoded pixels at the source’s supported depth, not the bytes of the PNG and WebP files. The containers are different even when the images they decode are the same.
