Save 8-bit pixels unchanged. Use swatches.png from the download.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
Wrote lossless.webpQuality = 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
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:
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.
using OneImage; var webp = new WebPEncoderOptions { Lossless = true, Effort = 6 }; Image.Load("swatches.png").Save("effort-6.webp", new SaveOptions { EncoderOptions = webp });
Related recipes
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 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.