All formats

ImageSharp

LanguageC# (.NET 8 or later)
LicenseSix Labors Split License: Apache 2.0 for open source projects and companies under $1M annual revenue, otherwise a commercial license
Repositorygithub.com/SixLabors/ImageSharp
Documentationdocs.sixlabors.com
Depends onnothing native (pure managed code)
Read / writeyes / yes, all color types, 1-16 bit, palette, text chunks, APNG

ImageSharp is a cross-platform image library written entirely in C#. It needs no native libraries, so the same code runs on Windows, Linux, macOS, in containers and on mobile. It reads and writes PNG, JPEG, GIF, BMP, TIFF, WebP, TGA, PBM, QOI and ICO, and has a processing API for resize, crop, rotate, color and drawing (drawing is in the separate SixLabors.ImageSharp.Drawing package).

The PNG encoder gives control over bit depth, color type, palette quantization, filter and compression level, and the decoder exposes text chunks, gamma and animation frames. Check the license before you use it in a commercial closed-source product.

Installation#

Shell
dotnet add package SixLabors.ImageSharp

Basic usage: read, resize and save a PNG#

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

using Image<Rgba32> image = Image.Load<Rgba32>("in.png");
Console.WriteLine($"{image.Width} x {image.Height}");

// width 400, height 0 keeps the aspect ratio
image.Mutate(x => x.Resize(400, 0));

image.SaveAsPng("out.png");

Image.Load detects the format from the file contents, so the same code opens JPEG, GIF or WebP files. Save("out.png") picks the encoder from the file extension.

How to get PNG width and height without decoding#

Image.Identify reads only the header:

C#
using SixLabors.ImageSharp;

ImageInfo info = Image.Identify("in.png");
Console.WriteLine($"{info.Width} x {info.Height}, {info.PixelType.BitsPerPixel} bits per pixel");

How to set PNG compression level and color type#

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;

using Image image = Image.Load("in.png");
var encoder = new PngEncoder
{
    CompressionLevel = PngCompressionLevel.BestCompression, // Level0 .. Level9
    ColorType = PngColorType.RgbWithAlpha,                  // Grayscale, Rgb, Palette, ...
    BitDepth = PngBitDepth.Bit8,
};
image.SaveAsPng("out.png", encoder);

How to save a palette (8-bit) PNG#

A palette PNG with 256 colors or fewer is often much smaller than RGBA. ImageSharp quantizes the image for you:

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing.Processors.Quantization;

using Image image = Image.Load("in.png");
image.SaveAsPng("palette.png", new PngEncoder
{
    ColorType = PngColorType.Palette,
    Quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = 256 }),
});

How to read and write PNG text chunks#

Text chunks (tEXt, zTXt, iTXt) are in the PNG format metadata:

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Png.Chunks;

using Image image = Image.Load("in.png");
PngMetadata png = image.Metadata.GetPngMetadata();
foreach (PngTextData text in png.TextData)
{
    Console.WriteLine($"{text.Keyword}: {text.Value}");
}

png.TextData.Add(new PngTextData("Software", "my app", "", ""));
image.SaveAsPng("out.png");

How to convert PNG to JPEG#

JPEG has no transparency, so fill transparent areas with a background color first:

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;

using Image image = Image.Load("in.png");
image.Mutate(x => x.BackgroundColor(Color.White));
image.SaveAsJpeg("out.jpg", new JpegEncoder { Quality = 85 });

Tips#