ImageSharp
| Language | C# (current .NET versions) |
|---|---|
| License | Six Labors Split License (Apache 2.0 for open-source and small companies, paid license for commercial use above a revenue threshold) |
| Repository | github.com/SixLabors/ImageSharp |
| Documentation | TIFF guide, API reference |
| Depends on | nothing (pure managed code) |
| Read / write | yes / yes, multi-frame, strips and tiles, BigTIFF read, LZW, Deflate, PackBits, JPEG, CCITT, 1-64 bits per pixel |
ImageSharp is a fully managed 2D imaging library for .NET. It loads TIFF and other formats into Image<TPixel>, processes them (resize, crop, rotate, color changes) and saves them in any supported format. Each TIFF page becomes a frame in image.Frames; saving writes all frames.
The Six Labors Split License is not an OSI license: it’s free for open-source projects and for companies under the revenue threshold in the license, others need a commercial license.
Installation#
dotnet add package SixLabors.ImageSharp
Basic usage: open a TIFF and save as PNG#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using Image image = Image.Load("in.tif"); // first page is the root frame
Console.WriteLine($"{image.Width} x {image.Height}, {image.Frames.Count} pages");
image.Mutate(x => x.Resize(image.Width / 2, 0)); // 0 keeps the aspect ratio
image.SaveAsPng("out.png");
How to save a TIFF with LZW compression#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Tiff;
using Image image = Image.Load("in.png");
image.SaveAsTiff("out.tif", new TiffEncoder {
Compression = TiffCompression.Lzw,
HorizontalPredictor = TiffPredictor.Horizontal,
BitsPerPixel = TiffBitsPerPixel.Bit24,
});
Other values: TiffCompression.Deflate (with CompressionLevel), PackBits, Jpeg, CcittGroup4Fax (for 1-bit images, with BitsPerPixel = TiffBitsPerPixel.Bit1).
How to read each page of a multi-page TIFF#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Image<Rgba32> doc = Image.Load<Rgba32>("scan.tif");
for (int i = 0; i < doc.Frames.Count; i++) {
using Image<Rgba32> page = doc.Frames.CloneFrame(i);
page.SaveAsPng($"page-{i}.png");
}
How to create a multi-page TIFF#
Add frames to an image, then save it as TIFF. Frames must have the same size as the root frame:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Image<Rgba32> doc = Image.Load<Rgba32>("page1.png");
foreach (string path in new[] { "page2.png", "page3.png" }) {
using Image<Rgba32> page = Image.Load<Rgba32>(path);
doc.Frames.AddFrame(page.Frames.RootFrame);
}
doc.SaveAsTiff("pages.tif");
How to read TIFF metadata#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Tiff;
ImageInfo info = Image.Identify("in.tif"); // reads headers only
TiffMetadata tiff = info.Metadata.GetTiffMetadata();
Console.WriteLine($"{info.Width} x {info.Height}, {tiff.Compression}, {tiff.BitsPerPixel}, {tiff.FormatType}, {tiff.ByteOrder}");
Console.WriteLine($"DPI: {info.Metadata.HorizontalResolution} ({info.Metadata.ResolutionUnits})");
// Exif tags such as Software and Artist
var exif = info.FrameMetadataCollection[0].ExifProfile;
FormatType tells you whether the file is classic TIFF or BigTIFF.
Tips#
Image.Identifygets size and metadata without decoding pixels.- Set
DecoderOptions.MaxFramesto decode only the first pages of a long document.