在 .NET 中替代 System.Drawing.Common 的 C# 位图库

Curtis Chau
Curtis Chau
更新: 2026年8月2日

IronDrawing 是免费的开源软件,并支持大多数 .NET 图形库格式

IronDrawing 是一个开源库,由 Iron Software 初步开发,帮助 C# 软件工程师替换在 .NET 项目中使用的 System.Drawing.Common,支持 Windows、macOS 和 Linux 平台。

为 .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 开发人员所依赖的代码库被弃用或过时。 我们必须立即行动!

我们的解决方案

一种用于在所有新旧图形库之间进行转换的中间图形格式。

作为类库开发者,您的公共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。

我们为什么要免费做这件事

我们开发 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 包快速且简单。 请按如下方式安装软件包:

PM > Install-Package IronSoftware.System.Drawing

或者,可以直接从官方 NuGet 网站下载。

安装后,您可以通过在 C# 代码顶部添加 using IronSoftware.Drawing; 开始使用。

代码示例

AnyBitmap 示例

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");

颜色示例

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}");

矩形示例

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);

字体示例

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}");
}

可提供的支持

许可

许可信息可在这里找到:LICENSE.txt

贡献

如果您想为 IronDrawing 开源项目贡献,请在向GitHub 仓库发起 pull-request 之前阅读许可协议。

信息

有关 Iron Software 的更多信息,请访问我们的网站:https://ironsoftware.com/

Iron Software 提供的支持

如需一般支持和技术咨询,请发送电子邮件给我们:support@ironsoftware.com

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 21,851,269版本:2026.11刚刚发布