System.Drawing.Common Alternatives (.NET 7 & Non-Windows)
In .NET6 and .NET7, Microsoft has stopped supporting Linux and MacOs, and System.Drawing.Common will only work on Windows.
- On non-Windows operating systems, a TypeInitializationException exception is thrown with PlatformNotSupportedException as the inner exception.
- In .NET 6, the platform analyzer emits compile-time warnings when referencing code is compiled for non-Windows operating systems. In addition, the following run-time exception is thrown unless you set a configuration option:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception. ---- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms.
{ "runtimeOptions": { "configProperties": { "System.Drawing.EnableUnixSupport": true } } }
Add the following code to the beginning of your code:
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
.NET7
With the release of .NET7, Microsoft has removed the work-around - completely removing support for System.Drawing.Common in Linux and MacOs.
Iron Software has released an open-source replacement to System.Drawing.Common, call IronSoftware.Drawing. [ NuGet] [GitHub]
To learn more: https://ironsoftware.com/open-source/csharp/drawing/docs/