Jan
27
2017

Using ImageMagick with Unity

(result image: add watermark/logo stamp to image)

Using ImageMagick with Unity is now possible with the .NET wrapper (github)

Tutorial: Add watermark to image

– Download required DLL files from https://magick.codeplex.com/releases/view/630360 (I used Magick.NET-7.0.4.400-Q8-x64.zip)
– From the zip file, copy “net20\Magick.NET\Magick.NET-Q8-x64.dll” into your Unity project Assets/Plugins/-folder
– From the zip file, copy “net20\Magick.NET\Magick.NET-Q8-x64.Native.dll” into your Unity project root folder (where the project .sln file is also)
– Copy “System.Drawing.dll” (v2.0) from your c: drive into your Unity project Assets/Plugins/-folder (I founded mine from C:\Windows\Microsoft.NET\Framework\v2.0.50727\)
– Download few sample images from Magick.NET examples here and copy those image files into your ‘Assets/Images/’ folder (or adjust the script below to point into correct image path)
– Add the c# script below to your scene and test it!
– See more tutorial sources at Magick github samples directory

example script (c#)


// using ImageMagick with unity : http://1darray.com/blog/2017/01/27/using-imagemagick-with-unity/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ImageMagick;
public class ImageMagickTest : MonoBehaviour
{
void Start()
{
CreateWatermark();
}
public void CreateWatermark()
{
// our image paths
var sourcePath = Application.dataPath + "/Images/FujiFilmFinePixS1Pro.jpg";
var watermarkPath = Application.dataPath + "/Images/Snakeware.png";
// Read image that needs a watermark
using (MagickImage image = new MagickImage(sourcePath))
{
// Read the watermark that will be put on top of the image
using (MagickImage watermark = new MagickImage(watermarkPath))
{
// Draw the watermark in the bottom right corner
image.Composite(watermark, Gravity.Southeast, CompositeOperator.Over);
// Optionally make the watermark more transparent
watermark.Evaluate(Channels.Alpha, EvaluateOperator.Divide, 4);
// Or draw the watermark at a specific location
image.Composite(watermark, 200, 50, CompositeOperator.Over);
}
// Save the result
image.Write(Application.dataPath + "/Images/" + "FujiFilmFinePixS1Pro-watermark.jpg");
}
}
}

view raw

ImageMagick.cs

hosted with ❤ by GitHub


13 Comments + Add Comment

  • Hi, just wondering if I will be able to make an app to publish for Android using this library on Unity?

    • havent tested, but i’m thinking the native library might not work.. going to try later.

  • That sounds really fantastic!
    But ImageMagick as a common graphic php library is capable of interpretation of most of graphic file formats, in the contrary to php GD library.
    Means it should enable to drag the svg, pdf, ai .tif files into Unity and display them as textures. Am I right?

    • that would be amazing! gotta test that soon..

  • Thank you for sharing Mgear. 🙂

  • Hey, you have tested for make an app for Android using this library? and read pdf files for display on textures?

  • Hello Mika, thanks a lot for this tutorial. I followed your steps and placed both .dll files in the appropriate folders, copied the System.Drawing.dll file into my Assets/Plugins/ folder, attached the ImageMagickTest.cs script to an empty game object and ran the game. I’m not getting any errors or warning messages, but unfortunately nothing happens. There is no 3rd image being created out of the original 2 images. I’ve searched many forums online and can’t seem to find a solution. Do you by any chance have this project in some repository that I could download? I’d love to take a closer look at a working, functional version of this. Thanks for your time!

    • did you try Refresh unity project window (right click, refresh) ?

      i tested on 2018.3.x seems to work directly also. i can try to put the project somewhere later.

      • Thanks for the response. I realized my issue had actually come from a small detail I missed. When I clicked Build and Run, my selected architecture was incompatible with the .dll, so changing this to the correct one fixed my issue. Cheers.

  • Works great in editor, and then when I do a standalone windows build I get:
    NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we’re attempting to marshal is: ImageMagick.StreamWrapper::Write

    any idea if there’s a workaround for this? It’s from the ToByteArray method here:

    Texture2D newTexture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
    newTexture.LoadImage(image.ToByteArray(MagickFormat.Png32));

  • Has anyone tried to make this multithreaded or use this with the job system? Would love to see an example if you’ve succeeded

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.