在 .NET 中用 C# 位图庫取代 System.Drawing.Common

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

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 圖形標準之間的無縫橋樑,隨著它們的發展 - 所以你只需要為你偏好的那一個開發。

IronDrawing 是免費、開源的,並支持大多數 .NET 繪圖庫格式

上下文

Microsoft .NET 是一個跨平台、高相容性的軟體架構,全球有數以百萬計的軟體開發者使用。Microsoft 已經 宣布 一個重大變更,即 System.Drawing.Common 將只支援 Windows 平台。

這對於維護使用 System.Drawing.Common 的跨平台庫的 .NET 庫開發人員來說是個問題,因為微軟 建議的行動 要求您重新構建庫以支持非 Windows 用戶。志願者和學術人員需要重建每個 NuGet 套件和類庫,使用每個新的建議圖形庫,這將導致技術債務,並減緩現代 .NET 的採用。

最終,當我們使用 .NET 時,我們使用 NuGet。

NuGet 套件開發者通過推進 .NET 和包裝代碼,這些代碼如果我們每天自己寫,將花費數千小時,對我們大家做了一個大忙。

這種重大變更將減緩 NuGet 的開發,並可能導致 .NET 開發者依賴的代碼庫被廢棄或變得過時。我們需要立即採取行動。!

我們的解決方案

一個能夠在所有舊的和新的圖形庫之間進行轉換的中介圖形格式。

作為一個類庫開發人員,您的公開 BitmapColorRectangleFontSize 的輸入和輸出只能是支持所有新標準的一種類型。在內部,您可以繼續做您喜歡的事情。

IronDrawing 與 .NET Framework 4.62 向下兼容,支持所有版本的 .NET (包括 .NET 8)IronDrawing 的開發和發布通過提供一個開源解決方案來解決這個對 BitmapColorRectangleFontSize 等重要類沒有統一格式的問題。IronSoftware.Drawing 將無縫地在 System.DrawingMicrosoft.MauiSkiaSharpSixLabors 中的這些類型的實現之間進行轉換。這使你,作為開發者,不需要在你的庫中替換這些類的所有實例。 範例,如果您正在使用 System.Drawing.Bitmap,您可以使用 IronDrawing 的 AnyBitmap 類,該類具有對以下類型的隱式轉換:System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharpMicrosoft.Maui.Graphics.Platform.PlatformImage

為什麼我們會免費提供這個服務

我們開發了 IronDrawing,因為我們 Iron Software 是長期資深的 .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: 一個通用的Color類別。在IronSoftware.Drawing.Color與下列所支援的類型之間進行隱式轉換:

    • System.Drawing.Color
    • SkiaSharp.SKColor
    • SixLabors.ImageSharp.Color
  • SixLabors.ImageSharp.PixelFormats
  • Rectangle: 一個通用的Rectangle類別。在IronSoftware.Drawing.Rectangle與下列所支援的類型之間進行隱式轉換:

    • System.Drawing.Rectangle
    • SkiaSharp.SKRect
    • SkiaSharp.SKRectI
  • SixLabors.ImageSharp.Rectangle
  • Font: 一個通用的Font類別。在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 套件簡單快速。請像這樣安裝套件:

Install-Package IronSoftware.System.Drawing

或者,直接從 官方 NuGet 網站一旦安裝完成,您可以在 C# 代碼的頂部添加 using IronSoftware.Drawing; 來開始使用。

程式碼範例

AnyBitmap 範例

:path=/static-assets/drawing/content-code-examples/get-started/anybitmap.cs
using 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")
VB   C#

顏色範例

:path=/static-assets/drawing/content-code-examples/get-started/color.cs
using 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()
VB   C#

矩形範例

:path=/static-assets/drawing/content-code-examples/get-started/rectangle.cs
using 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
VB   C#

字體範例

:path=/static-assets/drawing/content-code-examples/get-started/font.cs
using 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
VB   C#

支援可用

許可證

許可證資訊可在此找到: LICENSE.txt

Contribute

如果您想為 IronDrawing 開源項目做出貢獻,請閱讀 許可證 在進行 pull-request 之前 GitHub上的存儲庫.

信息

想了解更多關於 Iron Software 的信息,請訪問我們的網站:https://ironsoftware.com/

Iron Software 支援

如需一般支援及技術諮詢,請發送電子郵件至:mailto:support@ironsoftware.com