Magick.NET
| Language | C# (.NET Standard 2.0, .NET 8+, .NET Framework 4.6.2+) |
|---|---|
| License | Apache 2.0 (ImageMagick license for the bundled native library) |
| Repository | github.com/dlemstra/Magick.NET |
| Documentation | Magick.NET docs, ImageMagick TIFF options |
| Depends on | bundled native ImageMagick (with libtiff), per platform |
| Read / write | yes / yes, multi-page, 8/16/32-bit, CMYK, BigTIFF, all common compressions |
Magick.NET wraps ImageMagick, which uses libtiff for TIFF. It reads and writes nearly every TIFF variant, including CMYK, 16-bit and floating point, JPEG-compressed, Group 4 fax and multi-page files, and converts between TIFF and 100+ other formats (PDF needs Ghostscript installed). The NuGet package includes native binaries for Windows, Linux and macOS.
Pick the Q8 package for 8-bit images and the Q16 package (or Q16-HDRI) for 16-bit and floating point TIFF.
Installation#
Shell
dotnet add package Magick.NET-Q16-AnyCPU
Basic usage: convert a TIFF to PNG#
C#
using ImageMagick;
using var image = new MagickImage("in.tif"); // first page
Console.WriteLine($"{image.Width} x {image.Height}, {image.Compression}, {image.Depth}-bit");
image.Resize(800, 0); // 0 keeps the aspect ratio
image.Write("out.png");
How to save a TIFF with LZW or Group 4 compression#
C#
using ImageMagick;
using var image = new MagickImage("in.png");
image.Settings.Compression = CompressionMethod.LZW; // or Zip, JPEG, RLE (PackBits), Zstd
image.Write("out.tif", MagickFormat.Tiff);
// black-and-white fax compression
using var scan = new MagickImage("scan.png");
scan.Threshold(new Percentage(50));
scan.Settings.Compression = CompressionMethod.Group4;
scan.Write("scan.tif", MagickFormat.Tiff);
How to split a multi-page TIFF into pages#
C#
using ImageMagick;
using var pages = new MagickImageCollection("scan.tif");
int i = 0;
foreach (var page in pages) {
page.Write($"page-{i++}.png");
}
To read only one page, use new MagickImage("scan.tif", new MagickReadSettings { FrameIndex = 2, FrameCount = 1 }).
How to merge images into a multi-page TIFF#
C#
using ImageMagick;
using var doc = new MagickImageCollection();
foreach (var path in new[] { "page1.png", "page2.png", "page3.png" }) {
var page = new MagickImage(path);
page.Settings.Compression = CompressionMethod.LZW;
doc.Add(page); // the collection owns and disposes the images
}
doc.Write("pages.tif", MagickFormat.Tiff);
How to read TIFF tags and resolution#
ImageMagick exposes TIFF tags as tiff:* attributes:
C#
using ImageMagick;
using var image = new MagickImage("in.tif");
Console.WriteLine($"Software: {image.GetAttribute("tiff:software")}");
Console.WriteLine($"Photometric: {image.GetAttribute("tiff:photometric")}");
Console.WriteLine($"Byte order: {image.GetAttribute("tiff:endian")}");
Console.WriteLine($"Resolution: {image.Density}");
How to convert a multi-page TIFF to PDF#
C#
using var pages = new MagickImageCollection("scan.tif");
pages.Write("scan.pdf");
Tips#
- To write BigTIFF, use
MagickFormat.Tiff64(theTIFF64:format in ImageMagick). - Use
image.Settings.SetDefine(MagickFormat.Tiff, "predictor", "2")with LZW or Zip compression for smaller photos. MagickImageInforeads width, height and compression without decoding pixels.