All formats

SkiaSharp

LanguageC# (.NET, .NET Framework, Xamarin, MAUI)
LicenseMIT
Repositorygithub.com/mono/SkiaSharp
DocumentationSkiaSharp API reference
Depends onnative Skia library (shipped in the NuGet packages)
Read / writeyes / yes, 8-bit RGBA and grayscale; no access to text chunks

SkiaSharp wraps Skia, the 2D graphics engine used by Chrome, Android and Flutter. It decodes PNG, JPEG, GIF, BMP, ICO, WebP and more, and encodes PNG, JPEG and WebP. Its strength is drawing: text with real font shaping, paths, gradients, shadows and anti-aliasing, so it’s a good choice to generate images (charts, thumbnails with captions, badges).

The native Skia library is included in the NuGet package for Windows, macOS, iOS and Android. On Linux add SkiaSharp.NativeAssets.Linux (or .NoDependencies for containers without fontconfig). It doesn’t expose PNG metadata such as text chunks.

Installation#

Shell
dotnet add package SkiaSharp
# on Linux also:
dotnet add package SkiaSharp.NativeAssets.Linux

Basic usage: decode, resize and encode a PNG#

C#
using SkiaSharp;

using SKBitmap bitmap = SKBitmap.Decode("in.png");
if (bitmap == null)
{
    Console.Error.WriteLine("not an image");
    return;
}
Console.WriteLine($"{bitmap.Width} x {bitmap.Height}");

int width = 400;
int height = bitmap.Height * width / bitmap.Width;
using SKBitmap resized = bitmap.Resize(new SKImageInfo(width, height),
    new SKSamplingOptions(SKCubicResampler.Mitchell));

using FileStream output = File.Create("out.png");
resized.Encode(output, SKEncodedImageFormat.Png, 100); // quality is ignored for PNG

How to get PNG width and height without decoding#

SKCodec reads the header and doesn’t decode pixels:

C#
using SkiaSharp;

using SKCodec codec = SKCodec.Create("in.png");
SKImageInfo info = codec.Info;
Console.WriteLine($"{codec.EncodedFormat}: {info.Width} x {info.Height}, {info.ColorType}, {info.AlphaType}");

How to create a PNG with text and shapes#

C#
using SkiaSharp;

var info = new SKImageInfo(400, 200, SKColorType.Rgba8888, SKAlphaType.Premul);
using SKSurface surface = SKSurface.Create(info);
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.Transparent);

using var fill = new SKPaint { Color = SKColors.SteelBlue, IsAntialias = true };
canvas.DrawRoundRect(new SKRect(10, 10, 390, 190), 20, 20, fill);

using var font = new SKFont(SKTypeface.Default, 40);
using var textPaint = new SKPaint { Color = SKColors.White, IsAntialias = true };
canvas.DrawText("Hello PNG", 200, 115, SKTextAlign.Center, font, textPaint);

using SKImage image = surface.Snapshot();
using SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
File.WriteAllBytes("hello.png", data.ToArray());

How to convert PNG to JPEG#

Draw the PNG on a white background so transparent pixels don’t turn black:

C#
using SkiaSharp;

using SKBitmap png = SKBitmap.Decode("in.png");
using var jpg = new SKBitmap(png.Width, png.Height, SKColorType.Rgb888x, SKAlphaType.Opaque);
using (var canvas = new SKCanvas(jpg))
{
    canvas.Clear(SKColors.White);
    canvas.DrawBitmap(png, 0, 0);
}
using FileStream output = File.Create("out.jpg");
jpg.Encode(output, SKEncodedImageFormat.Jpeg, 85);

Tips#