Magick.NET
| Language | C# (.NET and .NET Standard 2.0) |
|---|---|
| License | Apache 2.0 (ImageMagick license for the native part; Ghostscript, needed to read PS, is AGPL-3.0) |
| Repository | github.com/dlemstra/Magick.NET |
| Documentation | docs in the repository |
| Depends on | native ImageMagick (bundled in the NuGet package); Ghostscript installed separately for reading PS |
| Read / write | yes (through Ghostscript) / yes (raster images wrapped in PostScript) |
Magick.NET bundles ImageMagick in a NuGet package. ImageMagick doesn’t interpret PostScript itself: to read a .ps file it calls Ghostscript, which renders each page to a bitmap, and you get one MagickImage per page. Writing works without Ghostscript: ImageMagick wraps the pixels in a PostScript program (formats Ps, Ps2, Ps3; Ps3 compresses the image data).
Pick Magick.NET when you want PostScript pages as images to resize, crop or convert in the same pipeline as other images. To convert PS to PDF with vector content preserved, use Ghostscript.NET with the pdfwrite device: Magick.NET would rasterize the pages.
Installation#
dotnet add package Magick.NET-Q8-AnyCPU
Install Ghostscript with the same bitness as your process (64-bit for x64). If you don’t want a system install, copy gsdll64.dll and gswin64c.exe next to your app and point Magick.NET to the folder:
MagickNET.SetGhostscriptDirectory(@"C:\MyApp\Ghostscript");
Basic usage: render all pages of a PostScript file to PNG#
using ImageMagick;
var settings = new MagickReadSettings
{
Density = new Density(150, 150), // dpi; the default 72 looks blurry
};
using var pages = new MagickImageCollection();
pages.Read("in.ps", settings);
int n = 1;
foreach (var page in pages)
{
page.Alpha(AlphaAction.Remove); // flatten onto white
page.Write($"page-{n:D3}.png");
n++;
}
How to render only one page#
FrameIndex is 0-based:
var settings = new MagickReadSettings
{
Density = new Density(300),
FrameIndex = 0, // first page
FrameCount = 1,
};
using var images = new MagickImageCollection();
images.Read("in.ps", settings);
images[0].Write("page1.png");
How to make a thumbnail of the first page#
var settings = new MagickReadSettings { Density = new Density(72), FrameIndex = 0, FrameCount = 1 };
using var image = new MagickImage("in.ps", settings);
image.Thumbnail(200, 200);
image.Write("thumb.jpg");
How to save an image as PostScript#
using var image = new MagickImage("photo.jpg");
image.Format = MagickFormat.Ps3; // Level 3, compressed image data
image.Write("photo.ps");
MagickFormat.Ps writes Level 1 (largest, works everywhere), Ps2 Level 2.
Tips#
- If reading fails with a “delegate” or “gswin64c” error, Ghostscript isn’t found: install it or call
MagickNET.SetGhostscriptDirectory. - Set
settings.ColorSpace = ColorSpace.sRGBto get RGB output from CMYK PostScript. - For several pages at high resolution, read one page at a time (
FrameIndex/FrameCount) to limit memory.