用於取代 .NET 中System.Drawing.Common的 C# 點陣圖庫

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 是一個跨平台、高度相容的軟體框架,被全球數百萬軟體開發人員使用。 微軟宣布了一項重大變更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.SKImage SixLabors.ImageSharp . Microsoft.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 -顏色:一種通用的顏色類別。 IronSoftware.Drawing.Color與下列型別之間支援隱含轉換:
    • System.Drawing.Color
    • SkiaSharp.SKColor
    • SixLabors.ImageSharp.Color
    • SixLabors.ImageSharp.PixelFormats
  • Rectangle :一個通用的矩形類別。 支援IronSoftware.Drawing.Rectangle與下列類型之間的隱含轉換:
    • System.Drawing.Rectangle
    • SkiaSharp.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 套件既快速又簡單。 請如下安裝軟體包:

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;
using System.IO;
using System.Drawing;

// Create a new AnyBitmap object from a file
// Replace "FILE_PATH" with the actual path of the image you want to process
var bitmap = AnyBitmap.FromFile("FILE_PATH");

// Save the AnyBitmap object as a JPEG file
bitmap.SaveAs("result.jpg");

// Export the AnyBitmap as a byte array
// This can be useful if you want to send the image data over a network or save it in a database
var bytes = bitmap.ExportBytes();

// Create a memory stream to export the AnyBitmap as a JPEG stream with 100% quality
using (var resultExport = new MemoryStream())
{
    // This exports the image to the stream with the specified format and quality
    bitmap.ExportStream(resultExport, AnyBitmap.ImageFormat.Jpeg, 100);
    // Be sure to use 'using' or manually close the stream to release resources
}

// Casting between System.Drawing.Bitmap and IronSoftware.Drawing.AnyBitmap
// Load a System.Drawing.Bitmap from the same file
System.Drawing.Bitmap image = new System.Drawing.Bitmap("FILE_PATH");

// This is an implicit conversion from System.Drawing.Bitmap to IronSoftware.Drawing.AnyBitmap
// This step is necessary if you are working with System.Drawing.Bitmap and need to convert to IronSoftware.Drawing.AnyBitmap for processing
IronSoftware.Drawing.AnyBitmap anyBitmap = AnyBitmap.FromBitmap(image);

// Save the resulting AnyBitmap from casting as a PNG file
anyBitmap.SaveAs("result-from-casting.png");
Imports IronSoftware.Drawing
Imports System.IO
Imports System.Drawing

' Create a new AnyBitmap object from a file
' Replace "FILE_PATH" with the actual path of the image you want to process
Private bitmap = AnyBitmap.FromFile("FILE_PATH")

' Save the AnyBitmap object as a JPEG file
bitmap.SaveAs("result.jpg")

' Export the AnyBitmap as a byte array
' This can be useful if you want to send the image data over a network or save it in a database
Dim bytes = bitmap.ExportBytes()

' Create a memory stream to export the AnyBitmap as a JPEG stream with 100% quality
Using resultExport = New MemoryStream()
	' This exports the image to the stream with the specified format and quality
	bitmap.ExportStream(resultExport, AnyBitmap.ImageFormat.Jpeg, 100)
	' Be sure to use 'using' or manually close the stream to release resources
End Using

' Casting between System.Drawing.Bitmap and IronSoftware.Drawing.AnyBitmap
' Load a System.Drawing.Bitmap from the same file
Dim image As New System.Drawing.Bitmap("FILE_PATH")

' This is an implicit conversion from System.Drawing.Bitmap to IronSoftware.Drawing.AnyBitmap
' This step is necessary if you are working with System.Drawing.Bitmap and need to convert to IronSoftware.Drawing.AnyBitmap for processing
Dim anyBitmap As IronSoftware.Drawing.AnyBitmap = AnyBitmap.FromBitmap(image)

' Save the resulting AnyBitmap from casting as a PNG file
anyBitmap.SaveAs("result-from-casting.png")
$vbLabelText   $csharpLabel

顏色範例

:path=/static-assets/drawing/content-code-examples/get-started/color.cs
using IronSoftware.Drawing;
using System;

// The IronSoftware.Drawing library provides enhanced color manipulation features.
// This example demonstrates creating color objects and converting between
// System.Drawing.Color and IronSoftware.Drawing.Color.

// Create a new Color object from a hex string.
Color fromHex = Color.FromHex("#191919");

// Create a new Color object from RGB values.
Color fromRgb = Color.FromRgb(255, 255, 0);

// Create a new Color object using an enumeration.
Color fromEnum = Color.Crimson;

// Casting between System.Drawing.Color and IronSoftware.Drawing.Color.
System.Drawing.Color drawingColor = System.Drawing.Color.Red;

// Convert System.Drawing.Color to IronSoftware.Drawing.Color.
IronSoftware.Drawing.Color ironColor = Color.FromSystemColor(drawingColor);

// Access the alpha, red, green, and blue components of the IronSoftware.Drawing.Color.
byte alpha = ironColor.A;
byte red = ironColor.R;
byte green = ironColor.G;
byte blue = ironColor.B;

// Calculate the luminance of the color.
// Luminance is a value from 0 (black) to 100 (white) where 50 is the perceptual "middle grey".
double luminance = ironColor.GetLuminance();

// Log the calculated attributes to the console.
Console.WriteLine($"Color Details - ARGB: ({alpha}, {red}, {green}, {blue}), Luminance: {luminance}");
Imports IronSoftware.Drawing
Imports System

' The IronSoftware.Drawing library provides enhanced color manipulation features.
' This example demonstrates creating color objects and converting between
' System.Drawing.Color and IronSoftware.Drawing.Color.

' Create a new Color object from a hex string.
Private fromHex As Color = Color.FromHex("#191919")

' Create a new Color object from RGB values.
Private fromRgb As Color = Color.FromRgb(255, 255, 0)

' Create a new Color object using an enumeration.
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

' Convert System.Drawing.Color to IronSoftware.Drawing.Color.
Private ironColor As IronSoftware.Drawing.Color = Color.FromSystemColor(drawingColor)

' Access the alpha, red, green, and blue components of the IronSoftware.Drawing.Color.
Private alpha As Byte = ironColor.A
Private red As Byte = ironColor.R
Private green As Byte = ironColor.G
Private blue As Byte = ironColor.B

' Calculate the luminance of the color.
' Luminance is a value from 0 (black) to 100 (white) where 50 is the perceptual "middle grey".
Private luminance As Double = ironColor.GetLuminance()

' Log the calculated attributes to the console.
Console.WriteLine($"Color Details - ARGB: ({alpha}, {red}, {green}, {blue}), Luminance: {luminance}")
$vbLabelText   $csharpLabel

矩形範例

:path=/static-assets/drawing/content-code-examples/get-started/rectangle.cs
using IronSoftware.Drawing;
using System.Drawing;

// Declare an IronSoftware.Drawing.Rectangle object
IronSoftware.Drawing.Rectangle ironRectangle = new IronSoftware.Drawing.Rectangle(5, 5, 50, 50);

// Declare a System.Drawing.Rectangle object
System.Drawing.Rectangle systemRectangle = new System.Drawing.Rectangle(10, 10, 150, 150);

// Implicitly convert System.Drawing.Rectangle to IronSoftware.Drawing.Rectangle
// Note: Uncomment and use appropriate conversion methods if available in the IronSoftware.Drawing library
// ironRectangle = (IronSoftware.Drawing.Rectangle)systemRectangle;

// Output the properties of IronSoftware.Drawing.Rectangle if conversion is successful
// These Console.WriteLine statements assume this code runs in a console environment
Console.WriteLine(ironRectangle.X);
Console.WriteLine(ironRectangle.Y);
Console.WriteLine(ironRectangle.Width);
Console.WriteLine(ironRectangle.Height);
Imports IronSoftware.Drawing
Imports System.Drawing

' Declare an IronSoftware.Drawing.Rectangle object
Private ironRectangle As New IronSoftware.Drawing.Rectangle(5, 5, 50, 50)

' Declare a System.Drawing.Rectangle object
Private systemRectangle As New System.Drawing.Rectangle(10, 10, 150, 150)

' Implicitly convert System.Drawing.Rectangle to IronSoftware.Drawing.Rectangle
' Note: Uncomment and use appropriate conversion methods if available in the IronSoftware.Drawing library
' ironRectangle = (IronSoftware.Drawing.Rectangle)systemRectangle;

' Output the properties of IronSoftware.Drawing.Rectangle if conversion is successful
' These Console.WriteLine statements assume this code runs in a console environment
Console.WriteLine(ironRectangle.X)
Console.WriteLine(ironRectangle.Y)
Console.WriteLine(ironRectangle.Width)
Console.WriteLine(ironRectangle.Height)
$vbLabelText   $csharpLabel

字體範例

:path=/static-assets/drawing/content-code-examples/get-started/font.cs
using System;
using System.Drawing;
using IronSoftware.Drawing;

// Create a new Font object with a specified font family, style, and size
IronSoftware.Drawing.Font font = new IronSoftware.Drawing.Font("Times New Roman", FontStyle.Italic | FontStyle.Bold, 30);

// Create a new instance of System.Drawing.Font
System.Drawing.Font drawingFont = new System.Drawing.Font("Courier New", 30);

try
{
    // Attempt to cast System.Drawing.Font to IronSoftware.Drawing.Font
    // Note: This cast may not be directly possible if the libraries do not support each other;
    // additional conversion logic might be required.
    IronSoftware.Drawing.Font ironFont = new IronSoftware.Drawing.Font(drawingFont.FontFamily.Name, drawingFont.Style, drawingFont.Size);

    // Accessing properties of the IronSoftware.Drawing.Font object
    string familyName = ironFont.FamilyName; // Get the font family name
    FontStyle style = ironFont.Style;       // Get the combined font style (italic, bold, etc.)
    float size = ironFont.Size;             // Get the font size
    bool isItalic = ironFont.Italic;        // Determine if the font style includes Italic
    bool isBold = ironFont.Bold;            // Determine if the font style includes Bold

    // Output the font properties to verify correctness
    Console.WriteLine($"Family: {familyName}, Style: {style}, Size: {size}, Italic: {isItalic}, Bold: {isBold}");
}
catch (InvalidCastException)
{
    Console.WriteLine("The conversion between System.Drawing.Font and IronSoftware.Drawing.Font is not directly supported.");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports System
Imports System.Drawing
Imports IronSoftware.Drawing

' Create a new Font object with a specified font family, style, and size
Private font As New IronSoftware.Drawing.Font("Times New Roman", FontStyle.Italic Or FontStyle.Bold, 30)

' Create a new instance of System.Drawing.Font
Private drawingFont As New System.Drawing.Font("Courier New", 30)

Try
	' Attempt to cast System.Drawing.Font to IronSoftware.Drawing.Font
	' Note: This cast may not be directly possible if the libraries do not support each other;
	' additional conversion logic might be required.
	Dim ironFont As New IronSoftware.Drawing.Font(drawingFont.FontFamily.Name, drawingFont.Style, drawingFont.Size)

	' Accessing properties of the IronSoftware.Drawing.Font object
	Dim familyName As String = ironFont.FamilyName ' Get the font family name
	Dim style As FontStyle = ironFont.Style ' Get the combined font style (italic, bold, etc.)
	Dim size As Single = ironFont.Size ' Get the font size
	Dim isItalic As Boolean = ironFont.Italic ' Determine if the font style includes Italic
	Dim isBold As Boolean = ironFont.Bold ' Determine if the font style includes Bold

	' Output the font properties to verify correctness
	Console.WriteLine($"Family: {familyName}, Style: {style}, Size: {size}, Italic: {isItalic}, Bold: {isBold}")
Catch e1 As InvalidCastException
	Console.WriteLine("The conversion between System.Drawing.Font and IronSoftware.Drawing.Font is not directly supported.")
Catch ex As Exception
	Console.WriteLine($"An error occurred: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

可提供的支持

許可證

許可資訊請參閱此處: LICENSE.txt

貢獻

如果您想為 IronDrawing 開源專案做出貢獻,請在向GitHub 上的儲存庫提交 pull request 之前閱讀許可證

資訊

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

Iron Software 提供的支持

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

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 13,852,431 | Version: 2025.3 剛發表