System.Drawing.Common Alternatives (.NET 7 & Non-Windows)
Workarounds to Avoid "System.Drawing.Common is not supported on this platform"
- Implementing System.Drawing.Common is not possible on Linux and macOS platforms for .NET 6 and .NET 7.
- You will receive an exception message indicating "System.Drawing.Common is not supported on this platform."
- Set the System.Drawing.EnableUnixSupport runtime configuration to true for .NET 6.
- Note that support is no longer available in .NET 7 as well.
- Use the IronSoftware.Drawing open-source library to replace System.Drawing.Common for .NET 7.
In .NET 6 and .NET 7, Microsoft has stopped supporting System.Drawing.Common on Linux and macOS. The library will only work on Windows platforms as per the official documentation.
- On non-Windows operating systems, a
TypeInitializationException
is thrown withPlatformNotSupportedException
as the inner exception. - In .NET 6, the platform analyzer emits compile-time warnings when code referencing System.Drawing.Common is compiled for non-Windows operating systems. Additionally, the following runtime exception is thrown unless a configuration option is set:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception. --- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms.
Temporary Work-around in .NET 6:
Enable support for non-Windows platforms by setting the System.Drawing.EnableUnixSupport
runtime configuration switch to true
in the runtimeconfig.json
file:
{
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
}
Add the following code to the beginning of your code to programmatically enable Unix support:
// Enables Unix support for System.Drawing in .NET 6. This setting is ignored in .NET 7.
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
// Enables Unix support for System.Drawing in .NET 6. This setting is ignored in .NET 7.
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
.NET 7
With the release of .NET 7, Microsoft has removed the work-around, completely removing support for System.Drawing.Common on Linux and macOS.
Iron Software has released an open-source replacement for System.Drawing.Common, called IronSoftware.Drawing. You can find it on NuGet and GitHub.
To learn more, you can visit the documentation page: IronSoftware Drawing Documentation