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

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 已经宣布了一个重大改动,即仅在 Windows 平台上支持 System.Drawing.Common

对于维护使用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

为什么我们免费做这个

我们在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:一个具有普遍兼容性的 Color 类。 在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;
​
// 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")
$vbLabelText   $csharpLabel

颜色示例

: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()
$vbLabelText   $csharpLabel

矩形示例

: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
$vbLabelText   $csharpLabel

字体示例

: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
$vbLabelText   $csharpLabel

可提供的支持

许可证

许可信息可以在此处找到:LICENSE.txt

贡献

如果您想为IronDrawing开源项目做出贡献,请在向GitHub上的代码库提交pull-request之前,阅读许可证

信息

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

Iron Software的支持

有关一般支持和技术查询,请通过以下电子邮件与我们联系:mailto:support@ironsoftware.com