System.Drawing.Common 替代方案(.NET 7 及非 Windows)
该平台不支持避免 system.drawing.common 的变通方法
1.在 .NET6 和 .NET7 的 Linux 和 MacOs 平台上无法实施 System.drawing.common
2.获取该平台不支持system.drawing.common的异常信息
3.为 .NET6 设置 System.Drawing.EnableUnixSupport 运行时配置为 true
4..NET7也不再提供支持
5.在.NET7中使用IronSoftware.Drawing开源库取代System.Drawing.Common。
在 .NET6 和 .NET7 中,微软停止了对 Linux 和 MacOs 的支持,并且 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.
.NET6 中的临时变通方法:
通过在 runtimeconfig.json 文件中将 System.Drawing.EnableUnixSupport 运行时配置开关设置为 true,启用对非 Windows 平台的支持:
{ "runtimeOptions": { "configProperties": { "System.Drawing.EnableUnixSupport": true } }}
在代码开头添加以下代码:
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
.NET7
随着 .NET7 的发布,Microsoft 取消了变通方法 - 在 Linux 和 MacOs 中完全取消了对 System.Drawing.Common 的支持。
Iron Software 发布了 System.Drawing.Common 的开源替代程序 IronSoftware.Drawing.Common。 [NuGet] [GitHub]
了解更多信息: https://ironsoftware.com/open-source/csharp/drawing/docs/