IRONSOFTWAREHOME

System.Drawing.Common 替代方案 (.NET 7 與非 Windows)

Curtis Chau
Curtis Chau
Updated: 2026年5月9日

避免"System.Drawing.Common 不支援此平台"的變通方法

  1. 在 .NET 6 和 .NET 7 的 Linux 和 macOS 平台上無法實現 System.Drawing.Common
  2. 您將收到一個例外訊息,表明"System.Drawing.Common 不支援此平台"。
  3. System.Drawing.EnableUnixSupport 執行時配置設定為 true,以支援 .NET 6。
  4. 請注意,.NET 7 中不再提供此支援。
  5. 使用 IronSoftware.Drawing 開源程式庫來取代 .NET 7 的 System.Drawing.Common

在 .NET 6 和 .NET 7 中,Microsoft 已停止在 Linux 和 macOS 上支援 System.Drawing.Common。 該程式庫將僅在 Windows 平台上運行,根據官方文件

  • 在非 Windows 作業系統上,會丟擲一個 TypeInitializationException 並以 PlatformNotSupportedException 作為內部例外。
  • 在 .NET 6 中,當為非 Windows 作業系統編譯引用 System.Drawing.Common 的程式碼時,平台分析器會發出編譯時警告。 此外,除非設置一個配置選項,否則會丟擲以下執行時例外:
System.TypeInitializationException : The type initializer for 'Gdip' threw an exception. --- System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms.
Text

.NET 6 的臨時變通方法:

透過在 runtimeconfig.json 檔案中設定 System.Drawing.EnableUnixSupport 執行時配置開關為 true,啟用對非 Windows 平台的支援:

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

在您的程式碼開頭新增以下程式碼,程式性地啟用 Unix 支援:

// 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

隨著 .NET 7 的發佈,Microsoft 已經移除了變通方法,完全移除在 Linux 和 macOS 上對 System.Drawing.Common 的支援。

Iron Software 發布了一個開源的替代方案,取代 System.Drawing.Common,稱為 IronSoftware.Drawing。 You can find it on NuGet and GitHub.

如需了解更多資訊,您可以造訪文件頁面:Iron Software Drawing Documentation

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 6,262,020版本:2026.9剛剛發布

立即獲取您的30天試用金鑰
無需信用卡或帳戶建立
Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預訂您的免費線上演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務咨詢
填寫下方表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立