IronBarcode - System.Drawing.Common 的替代方案
Iron Software 发布了 System.Drawing.Common 的开源替代品。欲了解更多有关 IronDrawing、 点击这里.
从 .NET 6.0 开始、 System.Drawing.Common 只能在 Windows 上运行.
- 在非 Windows 操作系统上,除非设置了运行时配置开关,否则会抛出 TypeInitializationException 异常,并将 PlatformNotSupportedException 异常作为内部异常。
- 在 .NET 6 中,当引用代码编译为非 Windows 操作系统时,平台分析器会发出编译时警告。此外,除非设置了配置选项,否则会抛出以下运行时异常:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception.
---- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
Stack Trace:
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(42,0): at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs(25,0): at System.Drawing.Bitmap..ctor(String filename)
/_/src/libraries/System.Resources.ResourceManager/tests/ResourceManagerTests.cs(270,0): at System.Resources.Tests.ResourceManagerTests.EnglishImageResourceData()+MoveNext()
/_/src/libraries/System.Linq/src/System/Linq/Select.cs(136,0): at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
----- Inner Stack Trace -----
/_/src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs(31,0): at System.Drawing.LibraryResolver.EnsureRegistered()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs(65,0): at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
/_/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs(27,0): at System.Drawing.SafeNativeMethods.Gdip..cctor()
临时解决办法:通过在 runtimeconfig.json 文件中将 System.Drawing.EnableUnixSupport 运行时配置开关设置为 true,启用对非 Windows 平台的支持:
{
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
}
该解决方案是 Microsoft 目前在 .NET 6 中支持 System.Drawing 的解决方案:
https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only