System.Drawing.Common Alternatives (.NET 7 & Non-Windows)

This article was translated from English: Does it need improvement?
Translated
View the article in English

從 .NET 6 開始,微軟已停止在 Linux 和 macOS 上支持 System.Drawing.Common。 該庫現在僅能在 Windows 上運行。 有關更多詳細資訊,請參閱官方文件

避免“System.Drawing.Common 不支持此平台”錯誤的解決方法

.NET 6

在非 Windows 操作系統上,會拋出 TypeInitializationException,而內部異常為 PlatformNotSupportedException。 平台分析器在非 Windows 平台上會發出編譯時警告。 除非您設定一項配置選項,否則將拋出以下運行時異常:

System.TypeInitializationException:類型初始化程序 'Gdip' 拋出異常。

---- System.PlatformNotSupportedException:System.Drawing.Common 不支持非 Windows 平台。

.NET 6 的臨時解決方法:

通過在 runtimeconfig.json 文件中將 System.Drawing.EnableUnixSupport 運行時配置開關設定為 true,啟用對非 Windows 平台的支持:

{
    "runtimeOptions": {
        "configProperties": {
            "System.Drawing.EnableUnixSupport": true
        }
    }
}

此外,在代碼的開頭加上以下行以開啟程序配置:

// Enable System.Drawing.Common support on non-Windows platforms in .NET 6
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
// Enable System.Drawing.Common support on non-Windows platforms in .NET 6
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
' Enable System.Drawing.Common support on non-Windows platforms in .NET 6
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", True)
$vbLabelText   $csharpLabel

.NET 7 及以上

從 .NET 7 開始,微軟已完全移除在 Linux 和 macOS 上對 System.Drawing.Common 的支持,包括之前可用的解決方法。

Iron Software 發布了一個開源的 System.Drawing.Common 替代,稱為 IronSoftware.Drawing

若欲了解更多,請訪問官方文件

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布