ImageSharp
| Language | C# (.NET 8 and later) |
|---|---|
| License | Six Labors Split License: Apache 2.0 for open source and companies under $1M annual revenue, commercial license otherwise (not OSI-approved as a whole) |
| Repository | github.com/SixLabors/ImageSharp |
| Documentation | docs.sixlabors.com |
| Depends on | nothing (pure managed code) |
| Read / write | yes / yes, lossy, lossless, alpha, animation |
ImageSharp implements WebP in C# without libwebp, so it runs anywhere .NET runs (Linux containers, Azure Functions, ARM) with no native packages. It decodes lossy, lossless and animated files and encodes all three. Encoding is slower than libwebp and files are slightly larger at the same quality setting.
Check the license before using it in a commercial product: companies with more than $1M annual revenue need a paid license.
Installation#
Shell
dotnet add package SixLabors.ImageSharp
Basic usage: convert an image to WebP#
C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Processing;
using Image image = Image.Load("photo.jpg"); // detects the format from the content
Console.WriteLine($"{image.Width} x {image.Height}");
image.Mutate(x => x.Resize(image.Width / 2, 0)); // 0 keeps the aspect ratio
image.SaveAsWebp("photo.webp", new WebpEncoder
{
FileFormat = WebpFileFormatType.Lossy,
Quality = 80, // 0-100, default 75
Method = WebpEncodingMethod.BestQuality, // 0 (fastest) .. 6 (smallest files)
});
Reading a WebP is the same Image.Load("in.webp"); image.SaveAsPng("out.png") converts it.
How to save a lossless WebP#
C#
using Image image = Image.Load("logo.png");
image.SaveAsWebp("logo.webp", new WebpEncoder
{
FileFormat = WebpFileFormatType.Lossless,
NearLossless = false, // true + NearLosslessQuality < 100 gives smaller, almost lossless files
});
How to get WebP dimensions and format without decoding#
Image.Identify reads only the headers:
C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Webp;
ImageInfo info = Image.Identify("in.webp");
WebpMetadata webp = info.Metadata.GetWebpMetadata();
Console.WriteLine($"{info.Width} x {info.Height}, {webp.FileFormat}, frames: {info.FrameMetadataCollection.Count}");
How to read the frames of an animated WebP#
C#
using Image image = Image.Load("anim.webp");
Console.WriteLine($"frames: {image.Frames.Count}, repeat: {image.Metadata.GetWebpMetadata().RepeatCount}");
for (int i = 0; i < image.Frames.Count; i++)
{
WebpFrameMetadata meta = image.Frames[i].Metadata.GetWebpMetadata();
Console.WriteLine($"frame {i}: {meta.FrameDelay} ms");
using Image frame = image.Frames.CloneFrame(i);
frame.SaveAsPng($"frame{i}.png");
}
How to create an animated WebP#
C#
using Image<Rgba32> anim = new(200, 200, Color.Red.ToPixel<Rgba32>());
anim.Frames.RootFrame.Metadata.GetWebpMetadata().FrameDelay = 500;
using (Image<Rgba32> blue = new(200, 200, Color.Blue.ToPixel<Rgba32>()))
{
ImageFrame<Rgba32> frame = anim.Frames.AddFrame(blue.Frames.RootFrame);
frame.Metadata.GetWebpMetadata().FrameDelay = 500;
}
anim.Metadata.GetWebpMetadata().RepeatCount = 0; // loop forever
anim.SaveAsWebp("anim.webp");
Rgba32 is in SixLabors.ImageSharp.PixelFormats.
Tips#
- Converting an animated GIF:
Image.Load("anim.gif").SaveAsWebp("anim.webp")keeps all frames and delays. - Use
Image.LoadAsync/SaveAsWebpAsyncin ASP.NET to avoid blocking threads. - Limit memory for untrusted uploads with
DecoderOptions { MaxFrames = 1 }or aConfigurationwith a smallerMemoryAllocator.