Search code examples
asp.netasp.net-mvc-4access-deniedmagick.net

System.UnauthorizedAccessException: Access is denied. ImageMagick (Magick.NET)


I am using Magick.NET-AnyCPU and it works great locally. When I deploy to my server it doesn't work and I get an exception logged saying:

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Stack trace
at ImageMagick.MagickImage..ctor()
at ImageMagick.MagickImage..ctor(Stream stream)

After doing a little digging it seems the AnyCPU version of the library attempts to dynamically load the version it needs from the Temp directory when it launches. Any help on how to get this fixed would be appreciated. I have tried to update permissions but hard to say if that is the cause or not. I was trying to use the x64 version of the library but then I can't even build my project because it references the System framework which I guess is x86.


Solution

  • As stated in the documentation (https://magick.codeplex.com/documentation) the AnyCPU version will write the x64 or the x86 version to your temporary directory. But this won't always work in a production environment. You can use the CacheDirectory property of the MagickAnyCPU class to change the location of the temporary directory.