在 Python 代码中使用 Python.NET 引用 SixLabors.ImageSharp 的 C# dll 时出现问题

问题描述 投票:0回答:1

我在让 PythonNet 使用引用 SixLabors.ImageSharp (v3.1.3) 的 C# dll 时遇到问题。我的最小 C# 代码(.NET 7.0 类库,编译成 dll)如下所示。它包含三个测试方法,RunTest1、RunTest2和RunTest3。

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace PythonNetTest
{
    public class TestClass
    {
        public void RunTest1()
        {
            Console.WriteLine("Test PythonNet");
        }

        public Image RunTest2()
        {
            return null;
        }

        public Image RunTest3()
        {
            return new Image<Rgba32>(100, 100);
        }
    }
}

我用来加载和使用这个 C# dll 的 Python 代码如下:

import os
import sys
import clr

from System.Reflection import Assembly
from System.IO import File
from System import Activator

clr.AddReference(r'<Path to ImageSharp DLL>\SixLabors.ImageSharp.dll')

path = r'<Path to C# dll>\PythonNetTest.dll'

dll = Assembly.Load(File.ReadAllBytes(path))

obj_type = dll.GetType('PythonNetTest.TestClass')

obj = Activator.CreateInstance(obj_type)

obj.RunTest1()

v2=obj.RunTest2()

v3=obj.RunTest3()

当我运行 Python 代码时,RunTest1 正确打印字符串“Test PythonNet”,并且 RunTest2 正确返回空对象。

RunTest3 但是失败,Python 控制台中出现以下异常:


TypeLoadException: Could not load type 'System.Environment' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at SixLabors.ImageSharp.Configuration..ctor(IImageFormatConfigurationModule[] configurationModules)
   at SixLabors.ImageSharp.Configuration.CreateDefaultInstance()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at SixLabors.ImageSharp.Configuration..cctor()


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File ~\miniconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\<***>\documents\untitled0.py:31
    v3=obj.RunTest3()

TypeInitializationException: The type initializer for 'SixLabors.ImageSharp.Configuration' threw an exception. ---> System.TypeLoadException: Could not load type 'System.Environment' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at SixLabors.ImageSharp.Configuration..ctor(IImageFormatConfigurationModule[] configurationModules)
   at SixLabors.ImageSharp.Configuration.CreateDefaultInstance()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at SixLabors.ImageSharp.Configuration..cctor()
   --- End of inner exception stack trace ---
   at SixLabors.ImageSharp.Image`1..ctor(Int32 width, Int32 height)
   at PythonNetTest.TestClass.RunTest3()

导致此异常的原因可能是什么?我使用的是直接下载的 ImageSharp dll。我没有使用 ImageSharp 源代码来构建 ImageSharp dll。不过,我确实查看了ImageSharp源代码,似乎它使用了一些环境函数,例如Environment.ProcessorCount和Environment.Newline。我认为这些来自 System.Runtime。

如有任何帮助,我们将不胜感激。

c# python.net sixlabors.imagesharp
1个回答
0
投票

我自己想出来了。我正在发布答案,以防对其他人有帮助。

我必须修改我的Python代码如下:

from pythonnet import load
load('coreclr')

import os
import sys
import clr

from System.Reflection import Assembly
from System.IO import File
from System import Activator

clr.AddReference(r'<Path to ImageSharp DLL>\SixLabors.ImageSharp.dll')

path = r'<Path to C# dll>\PythonNetTest.dll'

dll = Assembly.Load(File.ReadAllBytes(path))

obj_type = dll.GetType('PythonNetTest.TestClass')

obj = Activator.CreateInstance(obj_type)

obj.RunTest1()

v2=obj.RunTest2()

v3=obj.RunTest3()

基本上,显式加载“coreclr”。希望这对其他人有用。

© www.soinside.com 2019 - 2024. All rights reserved.