Magick.NET Documentation
repository·main·Indexed 26 days ago
https://github.com/dlemstra/magick.netA .NET wrapper for ImageMagick that provides image processing capabilities including reading, writing, resizing, and converting over 100 major file formats. It supports .NET 8.0 and .NET Standard 2.0, offering multiple quantum depth versions (Q8, Q16, Q16-HDRI) and platform-specific or AnyCPU NuGet packages. The library enables advanced operations such as creating animated GIFs, merging images into mosaics, and performing CMYK to RGB color space conversions.
What's inside Magick.NET
- Magick.NET is a .NET wrapper for ImageMagick, a powerful image manipulation library that supports over 100 major file formats. It allows you to use ImageMagick capabilities within your .NET applications without requiring a standalone installation of ImageMagick on your server or desktop.
Debug Magick.Native code within Magick.NET
mainTo enable debugging of the native
Magick.Nativelibrary while working in the Magick.NET project, follow these steps:- Directory Structure: Clone
Magick.Nativeat the same level asMagick.NET. For example:C:\Projects\Magick.NETC:\Projects\Magick.Native
- Clone ImageMagick: Inside the
Magick.Nativeproject, run the checkout script to clone ImageMagick:- Navigate to
Magick.Native\src\ImageMagickand runcheckout.cmd.
- Navigate to
- Create Debug Build: Use the scripts located in
src/Magick.Native/buildto create a debug build of the native library that can be stepped through while debugging Magick.NET.
- Directory Structure: Clone
Add support for other image formats
mainMagick.NET uses a statically linked build of ImageMagick for portability. If you need to support formats that are currently unsupported due to licensing or compatibility issues, you must create a custom build of the native library by modifying theBuild.Linux.shorBuild.macOS.shscripts.Handle Magick.NET exceptions
mainWhen performing image operations that may fail (such as reading invalid or corrupt files), wrap your code in a
try-catchblock. You can catch the baseMagickExceptionto handle any error raised by the library, or catch specific exceptions likeMagickCorruptImageErrorExceptionfor more granular error handling.try { // Read invalid jpg file using var image = new MagickImage(SampleFiles.InvalidFileJpg); } // Catch any MagickException catch (MagickException exception) { // Write exception raised when reading the invalid jpg to the console Console.WriteLine(exception.Message); } try { // Read corrupt jpg file using var image = new MagickImage(SampleFiles.CorruptImageJpg); } // Catch only MagickCorruptImageErrorException catch (MagickCorruptImageErrorException exception) { // Write exception raised when reading the corrupt jpg to the console Console.WriteLine(exception.Message); }Use Magick.NET with .NET Core on Linux
mainWhen running Magick.NET with .NET Core on Linux, add the appropriate Magick.NET package as a reference in your project file (
.csproj).<ItemGroup> <PackageReference Include="Magick.NET-Q8-x64" Version="7.x.x.x" /> </ItemGroup>Install required software for specific formats
mainCertain image formats require additional external software to be installed on your system:
- AI, EPS, PDF, and PS: Requires Ghostscript.
- Video (AVI, MP4, etc.): Requires FFmpeg.
Install Magick.NET via NuGet
mainMagick.NET is available as a NuGet package for C#, VB.NET, and .NET Core applications. You can choose between platform-specific packages to reduce application size or AnyCPU packages if the target platform is unknown. The library supportsnet8.0andnetstandard20.Configure fonts on Linux and macOS
mainMagick.NET relies on thefontconfiglibrary to read fonts on Linux and macOS. You must ensurefontconfigis installed on the host system or within your container. You may also need to runfc-cacheto update the system font cache.Choose the correct Magick.NET version (Q8, Q16, or Q16-HDRI)
mainSelect a version based on your precision and resource requirements:
- Q8: 8 bits-per-pixel component. This is the recommended version for most use cases.
- Q16: 16 bits-per-pixel component. Use this if you need to read or write 16-bit images without losing precision. It requires twice the resources of Q8.
- Q16-HDRI: Uses floating point (32 bits-per-pixel component) and allows out-of-bound pixels (less than 0 and more than 65535). It requires twice the memory of Q16.
Initialize Magick.NET with a directory of XML configuration files
mainYou can initialize the library by providing a path to a directory containing all required XML configuration files. This directory must contain all necessary XML files (typically a combination of files from VisualMagick's bin folder and Magick.Native's xml folder).
using System; using ImageMagick; namespace MagickExample { internal class Program { internal static void Main(string[] args) { MagickNET.Initialize(@"C:\MyProgram\MyImageMagickXmlFiles"); } } }Perform simple initialization of Magick.NET
mainTo ensure the ImageMagick library is properly initialized, call
MagickNET.Initialize()at the start of your application.using System; using ImageMagick; namespace MagickExample { internal class Program { internal static void Main(string[] args) { MagickNET.Initialize(); } } }Compile Magick.NET.Native on Linux and macOS
mainTo create a custom build of the native library on Linux or macOS, clone the Magick.NET repository and run the provided build scripts located in the
Source/Magick.NET.CrossPlatformfolder. These scripts create a statically linked library.- Linux:
Build.Linux.sh - macOS:
Build.macOS.sh
- Linux: