dlemstra / Magick.NET

The .NET library for ImageMagick
Apache License 2.0
3.48k stars 415 forks source link

Libraarty running under Avalonia XPF. #1742

Open mauisoftware opened 3 weeks ago

mauisoftware commented 3 weeks ago

Magick.NET version

14.0.0

Environment (Operating system, version and so on)

Avalonia XPF

Description

The following line crashes under Avalonia XPF Indie which requires Avalonia nightly builds (currently 11.3.999-cibuild0052834-alpha)...

var image = new MagickImage(imagePath);

System.TypeInitializationException: The type initializer for 'NativeMagickSettings' threw an exception. ---> System.EntryPointNotFoundException: Unable to find an entry point named 'Environment_Initialize' in DLL 'Magick.Native-Q8-x64.dll'. at ImageMagick.Environment.NativeMethods.X64.EnvironmentInitialize() at ImageMagick.Environment.NativeEnvironment.Initialize() in //src/Magick.NET/Generated/Magick.NET.SourceGenerator/ImageMagick.SourceGenerator.NativeInteropGenerator/Environment.g.cs:line 62 at ImageMagick.Environment.Initialize() in //src/Magick.NET/Helpers/Environment.cs:line 21 at ImageMagick.MagickSettings.NativeMagickSettings..cctor() in //src/Magick.NET/Generated/Magick.NET.SourceGenerator/ImageMagick.SourceGenerator.NativeInteropGenerator/MagickSettings.g.cs:line 299 --- End of inner exception stack trace --- at ImageMagick.MagickSettings.NativeMagickSettings.Create() in //src/Magick.NET/Generated/Magick.NET.SourceGenerator/ImageMagick.SourceGenerator.NativeInteropGenerator/MagickSettings.g.cs:line 333 at ImageMagick.MagickSettings..ctor() in //src/Magick.NET/Settings/MagickSettings.cs:line 33 at ImageMagick.MagickImage..ctor() in //src/Magick.NET/MagickImage.cs:line 43 at ImageMagick.MagickImage..ctor(String fileName) in //src/Magick.NET/MagickImage.cs:line 200

Steps to Reproduce

Call var image = new MagickImage(imagePath); under the mentioned conditions.

kekekeks commented 3 weeks ago

The issue is not related to Magick.NET. You've enabled the XPF Win32 API shim which by default installs a catch-all redirect for P/Invoke's. You need to manually whitelist ImageMagick as described in the linked doc, e. g.

AvaloniaUI.Xpf.WinApiShim.WinApiShimSetup.
  .AutoEnable(asm =>
  {
    var name = asm.GetName().Name.ToLowerInvariant();
    if (name is "magick.net" or "magick.net.core")
      return true;
    return false;
  });
mauisoftware commented 3 weeks ago

Thanks, I missed that in the Avalonia docs. Awesome product!