IronBarcode - System.Drawing.Common 的替代方案
Iron Software 提供了一個名為 IronDrawing 的 System.Drawing.Common 開源替代方案。 如欲進一步了解 IronDrawing,請點擊此處。
自 .NET 6 起,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

