WebP for .NET
A WebP encoder and decoder for .NET, written entirely in C# — lossy, lossless and animated .webp with alpha, in one managed assembly with no native code. Free on NuGet.
.NET 8, 9, 10 and 11 · One managed assembly · No native code · Free on NuGet
Install
Two packages. Darnelix.Webp is the library; Darnelix.Webp.Cli
is the webpx command-line tool, installed as a .NET tool. Both carry builds
for .NET 8, .NET 9, .NET 10 and .NET 11, and the tool needs one of those runtimes; where
only a newer .NET runtime is installed, webpx starts on that one. It is
tested on the four named here.
dotnet add package Darnelix.Webp dotnet tool install --global Darnelix.Webp.Cli As a local tool pinned to one repository instead: dotnet tool install Darnelix.Webp.Cli.
What ships
A WebP codec written entirely in C#, implemented from the published WebP container, VP8 and VP8L specifications. It decodes and encodes WebP — lossy, lossless, near-lossless, with alpha, still and animated — as one managed assembly with no native code and zero runtime dependencies.
-
Decode. Still images — lossy VP8 and lossless VP8L, with alpha — into RGBA32, BGRA32, ARGB32, RGB24 or BGR24; cropped and scaled decode; incremental decode from bytes as they arrive; header-only feature reads; animated WebP played frame by frame onto a composited canvas.
-
Encode. Still images, lossy (quality, effort, target size or PSNR, alpha quality and compression, sharp YUV, loop-filter settings) or lossless (effort, near-lossless preprocessing; the RGB under transparent pixels is always preserved); animated WebP assembled from full-canvas frames with per-frame durations, a loop count and a background colour.
-
Deterministic. The same input and options produce the same bytes — with or without the optional worker threads (
UseThreadson the decode and encode options, off by default). -
Defined failure. A truncated, corrupt or unsupported file raises a
WebpExceptionsubtype (WebpFormatException,WebpDecodeException,WebpUnsupportedFeatureException); a bad argument raises anArgumentException. -
SIMD-accelerated DSP kernels on every framework the package carries, each proven bit-identical to the scalar reference implementation they replace.
Two API styles cover the same functionality. The idiomatic API —
WebpDecoder, WebpEncoder, WebpAnimDecoder,
WebpAnimEncoder, each with an options type — exposes everything; the
static Webp class mirrors the shape of the reference C API's classic entry
points for the common cases (Decode, EncodeLossy,
EncodeLossless, GetFeatures). Container metadata (ICC, EXIF,
XMP) and animation parameters are edited with the command-line tool.
Quick start
Read a header without decoding, decode to RGBA32, encode lossless and lossy, then build the options yourself — the package README's quick start, as written.
using System;
using System.IO;
using Darnelix.Webp;
using Darnelix.Webp.Animation;
using Darnelix.Webp.Decoding;
using Darnelix.Webp.Encoding;
byte[] file = File.ReadAllBytes("input.webp");
// Header only — no pixels decoded; false (never an exception) when the bytes are not a WebP header.
if (WebpDecoder.TryGetInfo(file, out WebpImageInfo info))
{
Console.WriteLine($"{info.Width}x{info.Height} {info.Format} alpha={info.HasAlpha} animated={info.HasAnimation}");
}
// Decode. The image owns a pooled pixel buffer: dispose it when you are done with the pixels.
using (WebpImage image = WebpDecoder.Decode(file))
{
Span<byte> rgba = image.Pixels; // RGBA32, top-down, 4 bytes per pixel, no row padding
Console.WriteLine($"{image.Width}x{image.Height} {image.Format}, {rgba.Length} bytes");
}
// Encode RGBA32 pixels. The Webp facade applies the recommended defaults (effort 4, loop-filter
// strength 60, lossless alpha at effort 100); EncodeLossy takes the quality.
int width = 64, height = 64;
byte[] pixels = new byte[width * height * 4]; // RGBA32, top-down
byte[] lossless = Webp.EncodeLossless(pixels, width, height);
byte[] lossy = Webp.EncodeLossy(pixels, width, height, 85f);
File.WriteAllBytes("output.webp", lossy);
// For anything else, build the options yourself and set every field you rely on: a new
// WebpEncodeOptions starts at zero throughout, and zero means "off" for the loop filter and the
// alpha effort. Nothing is filled in for you.
var options = new WebpEncodeOptions { Quality = 85, Method = 4, FilterStrength = 60, AlphaQuality = 100, SharpYuv = true };
byte[] tuned = WebpEncoder.Encode(pixels, width, height, PixelFormat.Rgba32, options); Animated WebP, frame by frame:
using (var animation = new WebpAnimDecoder(file))
{
Console.WriteLine($"{animation.Info.CanvasWidth}x{animation.Info.CanvasHeight}, {animation.Info.FrameCount} frames");
while (animation.TryGetNextFrame(out WebpFrame frame))
{
// frame.Pixels is the full composited canvas (RGBA32); frame.DurationMs is its display time.
}
} The webpx command-line tool
The same codec drives webpx, packaged separately as
Darnelix.Webp.Cli: encode, decode, inspect, edit container metadata and
animation parameters, and build or extract animations from the shell.
webpx encode photo.ppm photo.webp -q 90 webpx encode- image → .webp — lossy or lossless, alpha, target size or PSNR
webpx decode- .webp → image — pixel layout, scale, crop, flip
webpx info- print the header features
webpx mux- set or strip ICC, EXIF and XMP, the loop count and the background colour
webpx demux- list chunks and frames; extract metadata
webpx anim build- frames → animated .webp
webpx anim extract- animated .webp → frames and durations
webpx version- print the codec version
Licence
The packages are free of charge. They are not open source. In plain words, for the packages as published by Darnelix on nuget.org, you may:
- use them in any application, service or workflow, commercial or not;
-
distribute the unmodified
Darnelix.Webpassembly inside your application; - copy and redistribute the unmodified packages, for example through mirrors, caches and private feeds.
You may not:
- modify, decompile or reverse engineer them, except where applicable law expressly permits it;
- sell, sublicense or rent the packages or their contents on their own, or offer them as part of a product whose main purpose is to provide WebP encoding or decoding to others as a library or tool;
-
use the library, or any assembly from the packages, in a Unity project, the Unity
Editor or a Unity build — that use is licensed only through WebP for Unity on the
Unity Asset Store. Running the
webpxtool as a separate program on a Unity project's files is allowed.
The full terms are in the LICENSE file packed with each package.
Answers
The problems this tool was built for, answered in full — with the code you would write yourself.
-
Convert WebP to PNG or JPEG, and back, in C#
Decode the WebP to raw pixels with a WebP codec and write them with a PNG or JPEG encoder; to go the other way, read the image to pixels and encode those as WebP.
-
Load a WebP into a System.Drawing Bitmap or a WPF image in C#
With .NET 8–11 on the Windows 11 builds tested, System.Drawing could not read or write WebP and WPF’s WebP decoder was a Store extension, so decode to BGRA yourself.
Using Unity? WebP for Unity is the same codec with an importer, components and samples.
Support
Questions and bug reports go to support@darnelix.com . Replies usually within a couple of business days.