SkiaSharp
| Language | C# (.NET, .NET Framework, MAUI, Xamarin) |
|---|---|
| License | MIT (Skia: BSD-3-Clause, libwebp: BSD-3-Clause) |
| Repository | github.com/mono/SkiaSharp |
| Documentation | SkiaSharp API reference |
| Depends on | native Skia library (SkiaSharp.NativeAssets.* packages, included for Windows and macOS) |
| Read / write | yes / yes, lossy and lossless still images; animated WebP read frame by frame |
SkiaSharp wraps Skia, the 2D graphics engine of Chrome and Android. Skia decodes and encodes WebP through a bundled libwebp, so WebP works on Windows, macOS, Linux, iOS and Android with the same code and libwebp speed. It’s the best .NET choice when you also draw text, shapes or gradients.
The encoder writes still images only; SKCodec can decode each frame of an animated WebP.
Installation#
Shell
dotnet add package SkiaSharp
# Linux servers and containers also need the native assets:
dotnet add package SkiaSharp.NativeAssets.Linux
Basic usage: decode and encode WebP#
C#
using SkiaSharp;
using SKBitmap bitmap = SKBitmap.Decode("in.webp"); // also PNG, JPEG, GIF, ...
if (bitmap == null) throw new Exception("cannot decode");
Console.WriteLine($"{bitmap.Width} x {bitmap.Height}");
using SKImage image = SKImage.FromBitmap(bitmap);
using SKData data = image.Encode(SKEncodedImageFormat.Webp, 80); // lossy, quality 0-100
using FileStream stream = File.OpenWrite("out.webp");
data.SaveTo(stream);
How to save a lossless WebP#
SKWebpEncoderOptions selects lossy or lossless compression:
C#
using SKBitmap bitmap = SKBitmap.Decode("logo.png");
using SKPixmap pixmap = bitmap.PeekPixels();
var options = new SKWebpEncoderOptions(SKWebpEncoderCompression.Lossless, 100); // quality = effort for lossless
using SKData data = pixmap.Encode(options);
File.WriteAllBytes("logo.webp", data.ToArray());
How to get WebP dimensions without decoding#
C#
using SKCodec codec = SKCodec.Create("in.webp");
SKImageInfo info = codec.Info;
Console.WriteLine($"{codec.EncodedFormat}: {info.Width} x {info.Height}, alpha: {info.AlphaType != SKAlphaType.Opaque}");
How to resize and convert JPEG to WebP#
C#
using SKBitmap src = SKBitmap.Decode("photo.jpg");
var info = new SKImageInfo(src.Width / 2, src.Height / 2);
using SKBitmap resized = src.Resize(info, new SKSamplingOptions(SKCubicResampler.Mitchell));
using SKData data = resized.Encode(SKEncodedImageFormat.Webp, 80);
File.WriteAllBytes("photo.webp", data.ToArray());
How to read the frames of an animated WebP#
C#
using SKCodec codec = SKCodec.Create("anim.webp");
SKCodecFrameInfo[] frames = codec.FrameInfo;
var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);
using var bitmap = new SKBitmap(info);
for (int i = 0; i < codec.FrameCount; i++)
{
// RequiredFrame tells which earlier frame this one is drawn on;
// decoding in order into the same bitmap handles that
codec.GetPixels(info, bitmap.GetPixels(), new SKCodecOptions(i));
Console.WriteLine($"frame {i}: {frames[i].Duration} ms");
}
Tips#
SKBitmap.Decodereturnsnullinstead of throwing for unsupported or broken files.- On Linux without system fonts use
SkiaSharp.NativeAssets.Linux.NoDependencies.