System.Drawing 中的 GDI+ 錯誤處理已更新為針對ExternalException錯誤擲回OutOfMemoryException,而非Status.OutOfMemory。
推出的版本
.NET 10
先前的行為
先前,當 GDI+ 遇到 Status.OutOfMemory 錯誤時 (通常是因為無效的輸入,而不是實際的記憶體問題) ,System.Drawing API 會擲回 OutOfMemoryException.
新行為
從 .NET 10 開始,當 GDI+ 遇到 Status.OutOfMemory 錯誤時,System.Drawing API 現在會擲回 ExternalException。
破壞性變更的類型
這是一種 行為改變。
變更的原因
GDI+ 在無法建立內部物件時,不太擅長傳回錯誤。 在許多情況下,由於輸入無效,物件建立失敗,更高層級的程式碼會取得 null 並將其轉換為 Status.OutOfMemory. 這通常是混亂的根源,因為錯誤通常與實際記憶體問題無關。
更改 ExternalException 可提供更準確的錯誤報告,因為這種例外狀況類型已在其他 System.Drawing 程式碼路徑中用於類似的 GDI+ 錯誤。
建議的動作
如果您的程式碼在使用 System.Drawing API 時擷取 OutOfMemoryException,確保您也擷取 ExternalException 以處理這些 GDI+ 錯誤。
try
{
// System.Drawing operations
}
catch (ExternalException ex)
{
// Handle GDI+ errors (including former OutOfMemoryException cases)
}
catch (OutOfMemoryException ex)
{
// Handle actual memory issues
}
Try
' System.Drawing operations
Catch ex As ExternalException
' Handle GDI+ errors (including former OutOfMemoryException cases)
Catch ex As OutOfMemoryException
' Handle actual memory issues
End Try
受影響的 API
所有可能與 GDI+ 互動並且先前可能產生OutOfMemoryExceptionStatus.OutOfMemory錯誤的 System.Drawing API,包括但不限於:
- System.Drawing.Bitmap 建構函式和方法
- System.Drawing.Graphics 方法
- System.Drawing.Image 方法
- System.Drawing.Icon 建構函式和方法
- 其他內部使用 GDI+ 的 System.Drawing 類型