How to embed Magick.NET to executable? #1554
-
I'm trying to create a console app using .NET Framework 4.8, with Visual Studio Community 2022 (17.9.0). OS is Windows 11 x64. In the app, Magick.NET-Q8-AnyCPU is used to get image's width with this code. public static int GetImageWidth(string path) {
try {
MagickImageInfo info = new MagickImageInfo(path);
if (info.Width <= 0) {
Console.Error.WriteLine("\x1b[31mERROR\x1b[0m Invalid width of image: '" + path + "'");
Environment.Exit(1);
return 0;
} else {
return info.Width;
}
} catch (Exception ex) {
Console.Error.WriteLine("\x1b[31mERROR\x1b[0m Failed to get width of image: '" + path + "'");
Console.Error.WriteLine(ex);
Environment.Exit(1);
return 0;
}
} Since I want to produce a single executable (no DLLs with executable to properly run), I merge all dependencies with ILMerge with MSBuild.ILMerge.Task. ILMerge packs all dependencies that 'Local Copy' is set to 'True' into executable built. But while other packed dependencies works as intended, Magick.NET does not.
I thought that DLL files are not properly merged, so I added these lines into <PropertyGroup>
<MagickCopyNativeWindows>true</MagickCopyNativeWindows>
<MagickCopyNativeLinux>true</MagickCopyNativeLinux>
<MagickCopyNativeLinuxMusl>true</MagickCopyNativeLinuxMusl>
<MagickCopyNativeMacOS>true</MagickCopyNativeMacOS>
</PropertyGroup> But this didn't change anything. Executable file size is same and I get same error with before. I checked if If I remove ILMerge, Magick.NET copies all DLLs into build directory, and works without any problem, but I don't want to put addtional DLL files with my executable, so ILMerge (or Costura.Fody) must be used. This is for MSBuild.ILMerge.Task. It doesn't work. {
"General": {
"InputAssemblies": [
"CommandLine.dll",
"Magick.Native-Q8-x64.dll",
"Magick.Native-Q8-x86.dll",
"Magick.NET.Core.dll",
"Magick.NET-Q8-AnyCPU.dll"
]
}
} For Costura.Fody, I tried with <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura>
<Unmanaged32Assemblies>
Magick.Native-Q8-x86
</Unmanaged32Assemblies>
<Unmanaged64Assemblies>
Magick.Native-Q8-x64
</Unmanaged64Assemblies>
</Costura>
</Weavers> How to fix this error? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You will need that extra Native library because this is native code instead. There might be tools for this but I am not aware of any tools that can do this. |
Beta Was this translation helpful? Give feedback.
You cannot merge these
.dll
files with ILMerge because that are native (C
) instead ofC#
. They will need to be in the same folder as your executable.