在.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 包应该很容易。

随着.NET 5、6、7和8图形标准的发展,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的开发和发布解决了这一问题,它提供了一个开源解决方案,解决了Bitmap, Color, Rectangle, FontSize等重要类缺乏统一格式的问题。IronSoftware.Drawing "将为您在 "System.Drawing"、"Microsoft.Maui"、"SkiaSharp "和 "SixLabors "中的这些类型的实现之间进行无缝转换。这样,开发人员就不必在库中替换这些类的所有实例。

对于 范例如果您使用的是 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(.NET 7\)所看到的那样。我们每天都在使用 NuGet,我相信您也一样。我们支持并希望鼓励向未来轻松过渡,摆脱 "System.Drawing"。

我们希望所有.NET类库和NuGet的开发都能变得更简单,这样NuGet生态系统就能继续蓬勃发展,所有.NET开发人员都能从中受益。

IronSoftware.Drawing 功能

  • AnyBitmap:普遍兼容的位图类。支持IronSoftware.Drawing.AnyBitmap和以下类之间的隐式转换:

    • 系统.绘图.位图

    • 系统.绘图.图像

    • SkiaSharp.SK位图

    • SkiaSharp.SKImage`图像

    • SixLabors.ImageSharp.
  • Microsoft.Maui.Graphics.Platform.PlatformImage `SkiaSharp.SKImage
  • **颜色通用兼容的颜色类。支持IronSoftware.Drawing.Color和以下类之间的隐式转换:

    • 系统.绘图.颜色

    • SkiaSharp.SKColor

    • SixLabors.ImageSharp.颜色
  • SixLabors.ImageSharp.PixelFormats 像素格式
  • **矩形普遍兼容的矩形类。支持IronSoftware.Drawing.Rectangle和以下类之间的隐式转换:

    • 系统.绘图.矩形

    • SkiaSharp.SKRect`类

    • SkiaSharp.SKRectI
  • SixLabors.ImageSharp.RectangleSkiaSharp.SKRectI`SkiaSharp.SKRectI
  • **字体普遍兼容的字体类。支持IronSoftware.Drawing.Font和以下类之间的隐式转换:

    • 系统.绘图.字体

    • SkiaSharp.SKFont.

    • SixLabors.Fonts.Font SkiaSharp.SKFont.

兼容性

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

捐款

如果您想为 IronDrawing 开源项目做出贡献,请阅读 许可证 在向 仓库.

信息

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

来自 Iron Software 的支持

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