C# Bitmap 庫以替代 System.Drawing.Common 在 .NET 中
IronDrawing 是免費、開源的,並支持大多數 .NET 繪圖庫格式
IronDrawing 是由 Iron Software 最初開發的一個開源庫,幫助 C# 軟體工程師在 Windows、macOS 和 Linux 平台上的 .NET 項目中替換 System.Drawing.Common。
開發支援圖形、圖像和字型的 .NET 5、6、7 和 8 的類別庫和 NuGet 套件 應該 是容易的。
IronDrawing 作為所有即將提出的 .NET 5、6、7 和 8 圖形標準之間的無縫橋樑,隨著它們的發展,您只需為您偏好的那一個進行開發。

避免發生 system.drawing.common 在此平台上不受支援例外的技術步驟
- Implement System.drawing.common is not possible for .NET6 and .NET 7 & 8 on Linux and MacOs platform
- 在這些平台上使用 System.Drawing.Common 將面臨異常消息
- 將System.Drawing.EnableUnixSupport運行時配置設置為true以支持 .NET6
- Support for System.Drawing.Common is no longer available in .NET 7 & 8
- 使用開源庫 IronSoftware.Drawing 來替代 System.Drawing.Common 以支持 .NET7
上下文
Microsoft .NET 是一個多平台、高度兼容的軟體框架,全球數百萬軟體開發者都在使用。 微軟已宣佈一個重大變更,其中System.Drawing.Common將僅支援 Windows 平台。
這對於維護跨平台庫且使用System.Drawing.Common的 .NET 庫開發人員來說是一個問題,因為微軟的建議措施要求您重新構建您的庫以支持非 Windows 用戶。 志工和學術界預期需要重建每個 NuGet 套件和類別庫,以使用每個新建議的圖形庫,隨著這些圖形庫的出現,這將導致技術債務,從而減緩現代 .NET 的採用。
最終,當我們使用 .NET 時,我們使用 NuGet。
NuGet 套件開發者透過推進 .NET 和預先打包的代碼,每天為我們節省了數千小時的編寫工作,對我們所有人都是一種幫助。
此重大變更將會減緩 NuGet 開發,並可能導致 .NET 開發人員所依賴的程式庫被遺棄或過時。 我們需要立即行動!
我們的解決方案
介於新舊圖形庫之間轉換的中介圖形格式。
作為類庫開發人員,您的公共 Bitmap、Color、Rectangle、Font 和 Size 輸入和輸出只能是一種類型,該類型支持所有新的標準。 在內部,你可以繼續做你喜歡的事情。
IronDrawing 向後相容於 .NET Framework 4.62,支持所有版本的 .NET(包括 .NET 8)。
IronDrawing的開發和發布解決了這一問題,通過提供一個開源解決方案,解決了Bitmap、Color、Rectangle、Font和Size等重要類缺乏統一格式的問題。 IronSoftware.Drawing 將為您無縫地在 System.Drawing、Microsoft.Maui、SkiaSharp 和 SixLabors 的實作之間進行轉換。 這允許您作為開發者,不必替換庫中這些類的所有實例。
例如,如果您正在使用 System.Drawing.Bitmap,您可以使用 IronDrawing 的 AnyBitmap 類,其具有到以下任何類型的隱式轉換:System.Drawing.Bitmap、System.Drawing.Image、SkiaSharp.SKBitmap、SkiaSharp.SKImage、SixLabors.ImageSharp、Microsoft.Maui.Graphics.Platform.PlatformImage。
為什麼我們免費做這個?
我們在 Iron Software 開發了 IronDrawing,因為我們是長期的資深 .NET 開發者,關心 .NET 的發展,並希望它能夠成長和成功。 我們很高興看到 .NET 在成長和進化,如同 .NET 7 所展現的。 我們整天都在使用 NuGet,我相信你也是。 我們支持並希望鼓勵輕鬆過渡到未來,並遠離System.Drawing。
我們希望所有 .NET 類庫和 NuGet 開發都能更加容易,以便 NuGet 生態系統繼續繁榮,並使所有 .NET 開發者受益。
IronSoftware.Drawing 功能
AnyBitmap:一個通用相容的 Bitmap 類別。 在IronSoftware.Drawing.AnyBitmap與下列支援項目之間的隱式轉換:System.Drawing.Bitmap
System.Drawing.Image
SkiaSharp.SKBitmap
SkiaSharp.SKImage- SixLabors.ImageSharp
- Microsoft.Maui.Graphics.Platform.PlatformImage
Color:一個通用的顏色類別。 在
IronSoftware.Drawing.Color和以下支持的类型之间进行隐式转换:System.Drawing.Color
SkiaSharp.SKColor- SixLabors.ImageSharp.Color
- SixLabors.ImageSharp.PixelFormats
Rectangle:一個通用相容的矩形類別。 在IronSoftware.Drawing.Rectangle和以下支持之間的隱式轉換:System.Drawing.RectangleSkiaSharp.SKRect- SkiaSharp.SKRectI
- SixLabors.ImageSharp.Rectangle
字型:通用相容的字型類別。 在
IronSoftware.Drawing.Font與以下支援的類型之間進行隱式轉換:System.Drawing.Font
SkiaSharp.SKFont- SixLabors.Fonts.Font
相容性
IronSoftware.Drawing具有與以下平台的跨平台支援相容性:
- .NET 8、.NET 7、.NET 6、.NET 5、.NET Core、.NET Standard 和 .NET Framework 4.62+
- Windows、macOS、Linux、Docker、Azure 和 AWS
安裝
安裝 IronDrawing (IronSoftware.Drawing) NuGet 套件既快速又簡單。 請按照以下方式安裝套件:
:InstallCmd Install-Package IronSoftware.System.Drawing:InstallCmd Install-Package IronSoftware.System.Drawing或者,直接從官方 NuGet 網站下載。
安裝完成後,您可以在您的 C# 代碼頂部添加 using IronSoftware.Drawing; 來開始。
请提供需要翻译的内容。
程式碼範例
AnyBitmap 範例
:path=/static-assets/drawing/content-code-examples/get-started/anybitmap.csusing IronSoftware.Drawing;
// Create a new AnyBitmap object
var bitmap = AnyBitmap.FromFile("FILE_PATH");
bitmap.SaveAs("result.jpg");
var bytes = bitmap.ExportBytes();
var resultExport = new System.IO.MemoryStream();
bitmap.ExportStream(resultExport, AnyBitmap.ImageFormat.Jpeg, 100);
// Casting between System.Drawing.Bitmap and IronSoftware.Drawing.AnyBitmap
System.Drawing.Bitmap image = new System.Drawing.Bitmap("FILE_PATH");
IronSoftware.Drawing.AnyBitmap anyBitmap = image;
anyBitmap.SaveAs("result-from-casting.png");Imports IronSoftware.Drawing
' Create a new AnyBitmap object
Private bitmap = AnyBitmap.FromFile("FILE_PATH")
bitmap.SaveAs("result.jpg")
Dim bytes = bitmap.ExportBytes()
Dim resultExport = New System.IO.MemoryStream()
bitmap.ExportStream(resultExport, AnyBitmap.ImageFormat.Jpeg, 100)
' Casting between System.Drawing.Bitmap and IronSoftware.Drawing.AnyBitmap
Dim image As New System.Drawing.Bitmap("FILE_PATH")
Dim anyBitmap As IronSoftware.Drawing.AnyBitmap = image
anyBitmap.SaveAs("result-from-casting.png")顏色範例
:path=/static-assets/drawing/content-code-examples/get-started/color.csusing IronSoftware.Drawing;
// Create a new Color object
Color fromHex = new Color("#191919");
Color fromRgb = new Color(255, 255, 0);
Color fromEnum = Color.Crimson;
// Casting between System.Drawing.Color and IronSoftware.Drawing.Color
System.Drawing.Color drawingColor = System.Drawing.Color.Red;
IronSoftware.Drawing.Color ironColor = drawingColor;
ironColor.A;
ironColor.R;
ironColor.G;
ironColor.B;
// Luminance is a value from 0 (black) to 100 (white) where 50 is the perceptual "middle grey"
IronDrawingColor.GetLuminance();Imports IronSoftware.Drawing
' Create a new Color object
Private fromHex As New Color("#191919")
Private fromRgb As New Color(255, 255, 0)
Private fromEnum As Color = Color.Crimson
' Casting between System.Drawing.Color and IronSoftware.Drawing.Color
Private drawingColor As System.Drawing.Color = System.Drawing.Color.Red
Private ironColor As IronSoftware.Drawing.Color = drawingColor
ironColor.A
ironColor.R
ironColor.G
ironColor.B
' Luminance is a value from 0 (black) to 100 (white) where 50 is the perceptual "middle grey"
IronDrawingColor.GetLuminance()矩形範例
:path=/static-assets/drawing/content-code-examples/get-started/rectangle.csusing IronSoftware.Drawing;
// Create a new Rectangle object
Rectangle Rectangle = new Rectangle(5, 5, 50, 50);
// Casting between System.Drawing.Rectangle and IronSoftware.Drawing.Rectangle
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(10, 10, 150, 150);
IronSoftware.Drawing.Rectangle ironRectangle = rectangle;
ironRectangle.X;
ironRectangle.Y;
ironRectangle.Width;
ironRectangle.Height;Imports IronSoftware.Drawing
' Create a new Rectangle object
Private Rectangle As New Rectangle(5, 5, 50, 50)
' Casting between System.Drawing.Rectangle and IronSoftware.Drawing.Rectangle
'INSTANT VB NOTE: The field rectangle was renamed since Visual Basic does not allow fields to have the same name as other class members:
Private rectangle_Conflict As New System.Drawing.Rectangle(10, 10, 150, 150)
Private ironRectangle As IronSoftware.Drawing.Rectangle = rectangle_Conflict
ironRectangle.X
ironRectangle.Y
ironRectangle.Width
ironRectangle.Height字體範例
:path=/static-assets/drawing/content-code-examples/get-started/font.csusing IronSoftware.Drawing;
// Create a new Font object
Font font = new Font("Times New Roman", FontStyle.Italic | FontStyle.Bold, 30);
// Casting between System.Drawing.Font and IronSoftware.Drawing.Font
System.Drawing.Font drawingFont = new System.Drawing.Font("Courier New", 30);
IronSoftware.Drawing.Font ironFont = drawingFont;
ironFont.FamilyName;
ironFont.Style;
ironFont.Size;
ironFont.Italic;
ironFont.Bold;Imports IronSoftware.Drawing
' Create a new Font object
Private font As New Font("Times New Roman", FontStyle.Italic Or FontStyle.Bold, 30)
' Casting between System.Drawing.Font and IronSoftware.Drawing.Font
Private drawingFont As New System.Drawing.Font("Courier New", 30)
Private ironFont As IronSoftware.Drawing.Font = drawingFont
ironFont.FamilyName
ironFont.Style
ironFont.Size
ironFont.Italic
ironFont.Bold支援可用
許可證
授權資訊可以在這裡找到:LICENSE.txt
參與貢獻
如果您想為 IronDrawing 開源專案做出貢獻,請在向 GitHub 上的儲存庫 提交 pull-request 之前閱讀許可證。
資訊
有關 Iron Software 的更多資訊,請訪問我們的網站:https://ironsoftware.com/
來自 Iron Software 的支援
如需一般支援和技術詢問,請透過以下電子郵件與我們聯繫:mailto:support@ironsoftware.com