Magick.NET Documentation

repository·main·Indexed 26 days ago

https://github.com/dlemstra/magick.net

A .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.

Tokens
13.8K
Snippets
35
Records
49
Agent score
87%

What's inside Magick.NET

  1. Overview of Magick.NET

    main
    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.
  2. Debug Magick.Native code within Magick.NET

    main

    To enable debugging of the native Magick.Native library while working in the Magick.NET project, follow these steps:

    1. Directory Structure: Clone Magick.Native at the same level as Magick.NET. For example:
      • C:\Projects\Magick.NET
      • C:\Projects\Magick.Native
    2. Clone ImageMagick: Inside the Magick.Native project, run the checkout script to clone ImageMagick:
      • Navigate to Magick.Native\src\ImageMagick and run checkout.cmd.
    3. Create Debug Build: Use the scripts located in src/Magick.Native/build to create a debug build of the native library that can be stepped through while debugging Magick.NET.
  3. Add support for other image formats

    main
    Magick.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 the Build.Linux.sh or Build.macOS.sh scripts.
  4. Handle Magick.NET exceptions

    main

    When performing image operations that may fail (such as reading invalid or corrupt files), wrap your code in a try-catch block. You can catch the base MagickException to handle any error raised by the library, or catch specific exceptions like MagickCorruptImageErrorException for 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);
    }
  5. Install Magick.NET via NuGet

    main
    Magick.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 supports net8.0 and netstandard20.
  6. Choose the correct Magick.NET version (Q8, Q16, or Q16-HDRI)

    main

    Select 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.
  7. Initialize Magick.NET with a directory of XML configuration files

    main

    You 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");
            }
        }
    }
  8. Perform simple initialization of Magick.NET

    main

    To 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();
            }
        }
    }
  9. Compile Magick.NET.Native on Linux and macOS

    main

    To 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.CrossPlatform folder. These scripts create a statically linked library.

    • Linux: Build.Linux.sh
    • macOS: Build.macOS.sh