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

ImageMagick.NET - Failed to load embedded x64 assembly

Jan 20, 2015 at 2:22 PM
It works fine in a local system. But in the live server, it throws an error like this

Failed to load embedded x64 assembly: unable to load file "c:WindowsTEMPMagick.Net."
Jan 20, 2015 at 3:02 PM
You probably don't have the correct access to: c:\Windows\TEMP\Magick.Net. You can specify an alternative directory with the CacheDirectory of MagickAnyCPU.
Feb 13, 2015 at 5:07 PM
Some users got similar error
so I have added this right after Initialization:
If (IsDirectoryWritable(Path.GetTempPath())) 
    string exepath = System.Reflection.Assembly.GetEntryAssembly().Location;
    var CachePath = Path.Combine(exepath, "Cache");
    if (!Directory.Exists(CachePath)) 
but I still receive crash reports:
System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'XX' that matches the specified binding constraints threw an exception. ---> System.InvalidOperationException: Failed to load embedded x64 assembly: unable to load file "C:\Users\james\AppData\Local\Temp\Magick.NET.\Magick.NET-Q16-x64.dll" ---> System.IO.FileNotFoundException: Could not load file or assembly 'Magick.NET-Q16-x64.dll' or one of its dependencies.
Can you please advise a solution?
Feb 13, 2015 at 6:19 PM
Did you install the Visual C++ Redistributable for Visual Studio 2012?
Feb 13, 2015 at 9:50 PM
My program would not work without it so I believe those users have it installed already.
Feb 13, 2015 at 10:12 PM
Believe or know?

And what do you mean by 'Initialization'? Are you calling any Magick.NET methods before the code where you set 'MagickAnyCPU.CacheDirectory'? This should be the first thing you do when your application starts.
Feb 13, 2015 at 11:00 PM
I don't know but I am pretty sure from some reports. I will try to contact some of those users.

That piece of code is right after window's InitializeComponent(); and before any Magick.NET methods. Exception is thrown later in code at first used Magick.NET method MagickNET.SupportedFormats()
Feb 13, 2015 at 11:05 PM
They probably need to reboot their machine after they installed the Visual C++ Redistributable for Visual Studio 2012. Can you ask them to do that? And since you are using the AnyCPU version your users should install both the x86 and the x64 version of the Visual C++ Redistributable.
Feb 14, 2015 at 11:27 PM
I got in touch with one user, the only one out of 10 who submitted crash report including his email address. He reinstalled both redistributables, restarted computer, ran as administrator and still gets above exception at the same line. He has Windows 7 with SP1.
Feb 15, 2015 at 1:58 AM
Please disregard until further notice. I have tested this on two old laptops and it worked after reinstalling redistributables. I have now pushed the update that will not even allow running program if msvcr110.dll is not present and it will display message to reinstall redistributables. I will wait now for any crash reports. It could be that some users are confused with that redistributables download where you select versions to download.
Feb 15, 2015 at 4:27 AM
I'm getting this same error on my Windows 7 (32bit) machine, and it was compiled on Windows 8 (64), but I have the AnyCPU dll. I installed / updated the C++ Redistributable...

Here's the error:

System.TypeInitializationException: The type initializer for 'SeaRisen.FrameITLib.Files.Images.ImageFactoryXL' threw an exception. ---> System.InvalidOperationException: Failed to load embedded x64 assembly: Failed to load embedded x64 assembly: unable to load file "C:\Users\SeaRisen\AppData\Local\Temp\Magick.NET.\Magick.NET-Q16-x64.dll" ---> System.IO.FileNotFoundException: Could not load file or assembly 'Magick.NET-Q16-x64.dll' or one of its dependencies. The specified module could not be found.
at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
at ImageMagick.AssemblyHelper.LoadAssembly(String cacheDirectory, String tempFile)
at ImageMagick.AssemblyHelper.LoadAssembly()

I tried renaming the Magick.NET. folder, it recreated it fine but still failed to load the DLL and I verified it exists. It saying "or one of its dependencies", what could that be?
Feb 15, 2015 at 11:34 AM
When you receive the message 'The specified module could not be found.' it most likely means it cannot find the C++ Redistributable. But your error is confusing me because you say you are on a 32bit Windows 7. The error message is about attempting to load an 64-bit assembly. This should never happen.
Feb 17, 2015 at 6:35 AM
Ok, you're right. I had thought it to be a 32 bit system, and I'd loaded the 32 bit C++ Redistributable. Installing the 64bit Redistributable solved the problem.
Jun 20, 2015 at 10:04 PM
Edited Jun 20, 2015 at 10:11 PM
I was finally able to reproduce the error. It happens only in release version, AnyCPU;
I am using this code:
string exepath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var CachePath = Path.Combine(exepath, "Cache");
if (!Directory.Exists(CachePath)) Directory.CreateDirectory(CachePath);
MagickAnyCPU.CacheDirectory = CachePath;
var test = MagickNET.SupportedFormats.Any();
Created folder has write access. It is empty.
System.BadImageFormatException: Could not load file or assembly 'Magick.NET.Wrapper-x86, Version=, Culture=neutral, PublicKeyToken=2004825badfa91ec' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'Magick.NET.Wrapper-x86, Version=, Culture=neutral, PublicKeyToken=2004825badfa91ec'
   at ImageMagick.MagickNET.<get_SupportedFormats>d__0.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at OneCommander.UserControlFileProcessor.InitialCheck() in c:\Users\Milos\Documents\bitCommander\bitCommander\UserControlFileProcessor.xaml.cs:line 104

=== Pre-bind state information ===
LOG: DisplayName = Magick.NET.Wrapper-x86, Version=, Culture=neutral, PublicKeyToken=2004825badfa91ec
LOG: Appbase = file:///C:/Users/Milos/Documents/bitCommander/bitCommander/bin/Release/
LOG: Initial PrivatePath = NULL
Calling assembly : Magick.NET-AnyCPU, Version=, Culture=neutral, PublicKeyToken=2004825badfa91ec.
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\Milos\Documents\bitCommander\bitCommander\bin\Release\OneCommander.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Magick.NET.Wrapper-x86, Version=, Culture=neutral, PublicKeyToken=2004825badfa91ec
LOG: Attempting download of new URL file:///C:/Users/Milos/Documents/bitCommander/bitCommander/bin/Release/Magick.NET.Wrapper-x86.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
Debug version runs fine on the same computer;
VS redistributables are installed of both x86 and x64. Happens on Windows 10, x64, but several users have reported the same on 7 and 8.
Jun 20, 2015 at 11:27 PM
Do you have the file 'Magick.NET.Wrapper-x86.dll' in your bin directory? It should not be there.
Jun 23, 2015 at 2:05 AM
Even though I have no idea how that file got there and why it is an issue I am glad the solution was that simple. Thank you very much!
Jun 23, 2015 at 6:10 AM
The AnyCPU version of Magick.NET has both 'Magick.NET.Wrapper-x86.dll' and 'Magick.NET.Wrapper-x64.dll' as an embedded resource. When the AnyCPU assembly tries to access a method from the Magick.NET.Wrapper library it will copy either the x86 or the x64 version to the cache directory and load it. But because you have 'Magick.NET.Wrapper-x86.dll' in your bin directory it can already find a version of the Magick.NET.Wrapper library there. It will try to load that file but it fails because your application pool is 64-bit and the library itself is 32-bit.
Jun 23, 2015 at 3:09 PM
Thank you for the explanation!