This project has moved and is read-only. For the latest updates, please go here.

Magick.NET and Mono

Jul 18, 2013 at 1:08 PM
So, does Magick.NET works on Mono?

1) yes, out of the box
2) must be recompiled to use with Mono
3) no, but Mono support is planned
4) no, noway!
5) other?
Jul 18, 2013 at 1:29 PM
Edited Jul 18, 2013 at 1:50 PM
no idea?

ImageMagick is cross platform, why would you want to use Magick.NET with Mono?

Edit:
This will probably only work on windows because Magick.NET is a mixed-mode assembly, more info: http://www.mono-project.com/CPlusPlus
Jul 19, 2013 at 6:11 AM
I see, the problem is in managed C++.

I want to use Magick.NET with Mono because Mono is cross-platform too.

Now for a simple cross-platform solution I need to create some sort of an object wrapper around imagemagick executable, but with a plenty of imagemagick command line options it is hard to create more or less complete API.
Jul 19, 2013 at 7:48 AM
Edited Jul 19, 2013 at 7:49 AM
I might create a build for Mono in the future when I have too much spare time on my hands. I don't know if it is possible but I will give it a try later.
Jul 19, 2013 at 8:44 AM
Thanks anyway!
Aug 10, 2013 at 1:10 PM
A GraphicsMagick library exists for Node.js which wraps the executable in a fluent interface: http://aheckmann.github.io/gm/. Maybe that idea could be ported to .NET to make a multiplatform library :)

Could you potentially use P/Invoke instead of a mixed mode assembly? Mono does support P/Invoke.
Aug 10, 2013 at 2:30 PM
I don't really want to switch to P/Invoke or wrap the executable because I am quite happy that everything is now included in one library that only depends on the C++ redistributable library. If I ever create a Mono version of Magick.NET I will probably have to generate a different set of ImageMagick header files that do a P/Invoke. But as I said earlier I first need to find some extra time :)
Aug 10, 2013 at 2:36 PM
That's fair enough, having it all as one library makes it easy to use :)

For what it's worth, here's the solution I came up with for resizing images using GraphicsMagick, which works on both MS.NET and Mono. I'm using GraphicsMagick to work around the horrible quality of resized images in Mono. Mono's GDI+/System.Drawing implementation uses Cairo which just does a bilinear resize with no nicer interpolation options.
/// <summary>
/// Generates a thumbnail for the specified image, using ImageMagick or GraphicsMagick
/// </summary>
/// <param name="sourceImg">Image to generate thumbnail for</param>
/// <param name="width">Width of the thumbnail</param>
/// <param name="height">Height of the thumbnail</param>
/// <returns>Thumbnail image</returns>
public static Bitmap GenerateMagickThumbnail(this Image sourceImg, int width, int height)
{
    // Create new GraphicsMagick process for thumbnail generation
    var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "gm",
            Arguments = string.Format("convert - -filter sinc -size {0}x{1} -resize {0}x{1} -", width, height),
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        }
    };

    process.Start();

    // Write source image to input stream of GraphicsMagick
    sourceImg.Save(process.StandardInput.BaseStream, ImageFormat.Png);
    process.StandardInput.Flush();
    process.StandardInput.Close();

    try
    {
        var thumb = new Bitmap(process.StandardOutput.BaseStream);
        return thumb;
    }
    catch (Exception ex)
    {
        var errors = process.StandardError.ReadToEnd();
        throw new Exception(string.Format("Error invoking GraphicsMagick: {0}\nOriginal exception: {1}", errors, ex));
    }
    finally
    {
        process.Dispose();
    }
}
Mar 9, 2015 at 2:27 PM
Just wondering, is there any update on Mono compatibility? Or are we holding tight until the CoreCLR goes prime time?
Mar 9, 2015 at 2:36 PM
I am making preparations to make this possible. Currently the C++ code is integrated with the library but I am planning to move this to a separate library. I have created a proof of concept here: https://github.com/dlemstra/MixedModeAnyCPU. After these changes have been made it will be possible to make a MagicNET.Wrapper that works on Linux/Unix. But not sure when I will be able to do that :)