There are three ways to initialize the ImageMagick library these are described below.
The example below shows how you can initialize the library and make sure the ImageMagick library is properly initialized.
using System;
using ImageMagick;
namespace MagickExample
{
internal class Program
{
internal static void Main(string[] args)
{
MagickNET.Initialize();
}
}
}
The example below show how you can initialize ImageMagick with a directory that contains all the xml configuration files. This directory will need to contain all the xml files. Those files can be found in the bin folder of VisualMagick combined with the files from the xml folder of Magick.Native.
using System;
using ImageMagick;
namespace MagickExample
{
internal class Program
{
internal static void Main(string[] args)
{
MagickNET.Initialize(@"C:\MyProgram\MyImageMagickXmlFiles");
}
}
}
Another option is to use the ConfigurationFiles class. This class contains a property for each of the xml files and returns an object that contains the file name and the data of the xml configuration file. Below is an example of how you could change the policy to only allow a specific set of formats.
using System;
using ImageMagick;
using ImageMagick.Configuration;
namespace MagickExample
{
internal class Program
{
internal static void Main(string[] args)
{
var configFiles = ConfigurationFiles.Default;
configFiles.Policy.Data = @"
<policymap>
<policy domain=""delegate"" rights=""none"" pattern=""*"" />
<policy domain=""coder"" rights=""none"" pattern=""*"" />
<policy domain=""coder"" rights=""read|write"" pattern=""{GIF,JPEG,PNG,WEBP}"" />
</policymap>";
string temporaryDirectory = MagickNET.Initialize(configFiles);
}
}
}
The method will return the temporary directory where the xml files were written to. This location can be controlled with extra an overload that allows you to specify the location.
var configFiles = ConfigurationFiles.Default;
MagickNET.Initialize(configFiles, @"C:\MyProgram\MyImageMagickXmlFiles");