C# Bitmap Library to Replace System.Drawing.Com/mon in .NET
IronDrawing 是免费的开源软件,支持大多数 .NET 绘图库格式
IronDrawing 是由 Iron Software 最初开发的开源库,旨在帮助 C# 软件工程师在 Windows、macOS 和 Linux 平台的 .NET 项目中替换 System.Drawing.Com/mon。
为 .NET 5、6、7 和 8 开发支持图形、图像和字体的类库及 NuGet 包,_理应_是一件轻松的事情。
随着所有新提出的 .NET 5、6、7 和 8 图形标准的不断演进,IronDrawing 充当了它们之间的无缝桥梁——因此您只需针对自己偏好的标准进行开发即可。
技术步骤:避免"system.drawing.com/mon 在此平台上不受支持"的异常
- Implement System.drawing.com/mon is not possible for .NET 6 and .NET 7 & 8 on Linux and macOS platforms
- 在这些平台上使用 System.Drawing.Com/mon 将会引发异常信息
- 对于 .NET 6,请将 System.Drawing.EnableUnixSupport 运行时配置设置为 true
- .NET 7 和 8 已不再支持 System.Drawing.Com/mon
- 请使用开源库 IronSoftware.Drawing 来替代 .NET 7 中的 System.Drawing.Com/mon
上下文
Microsoft .NET Framework 是一个多平台、兼容性极高的软件框架,全球数百万软件开发人员都在使用它。 微软已宣布一项重大变更,即 System.Drawing.Com/mon 仅在 Windows 平台上受支持。
这对维护使用 System.Drawing.Com/mon 的跨平台库的 .NET 库开发者来说是个难题,因为微软建议的解决方案要求您重新构建库以支持非 Windows 用户。 预计志愿者和学者将需要重新构建每个 NuGet 包和类库,以适配新推出的每款建议图形库,这将产生技术债务,从而延缓现代 .NET 的采用进程。
归根结底,使用 .NET 时,我们就会用到 NuGet。
NuGet 包开发者们通过推动 .NET 技术的发展,并提供预打包的代码,为我们所有人提供了极大便利——这些代码如果由我们自己编写,每天将耗费数千小时。
这一重大变更将减缓 NuGet 的开发进程,并可能导致 .NET 开发者所依赖的代码库被弃用或过时。 我们需要立即行动!
我们的解决方案
一种用于在所有旧版和新版图形库之间进行转换的中间图形格式。
作为类库开发者,您的公共 Font 和 Size 输入和输出只能采用一种支持所有新标准的类型。 在内部,您可以继续做自己喜欢的事情。
IronDrawing 向下兼容 .NET Framework 4.62,并支持所有版本的 .NET(包括 .NET 8)。
IronDrawing 的开发与发布通过提供开源解决方案,解决了重要类(如 Font 以及 Size 等重要类缺乏统一格式的问题。 IronSoftware.Drawing 将为您在 SkiaSharp 和 SixLabors 中的这些类型实现之间进行无缝转换。 这样,您作为开发者,就无需在库中替换所有这些类的实例。
例如,如果您正在使用 System.Drawing.Bitmap,可以使用 IronDrawing 的 AnyBitmap 类,该类支持向以下任何类型的隐式转换: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.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharpMicrosoft.Maui.Graphics.Platform.PlatformImage
- Color:一个具有普遍兼容性的 Color 类。 支持
IronSoftware.Drawing.Co/lor与以下内容之间的隐式转换:System.Drawing.Co/lorSkiaSharp.SKColorSixLabors.ImageSharp.Co/lorSixLabors.ImageSharp.PixelFormats
Rectangle:一个通用兼容的 Rectangle 类。 支持IronSoftware.Drawing.Rectangle与以下内容之间的隐式转换:System.Drawing.RectangleSkiaSharp.SKRectSkiaSharp.SKRectISixLabors.ImageSharp.Rectangle
- Font:一个具有广泛兼容性的 Font 类。 支持
IronSoftware.Drawing.Font与以下内容之间的隐式转换:System.Drawing.FontSkiaSharp.SKFontSixLabors.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")
颜色示例
: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}")
矩形示例
: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)
字体示例
: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
提供支持
许可协议
许可信息请参见此处:LICENSE.txt
贡献
如果您希望为 IronDrawing 开源项目做出贡献,请在向 GitHub 上的代码库提交拉取请求之前阅读许可协议。
信息
有关 Iron Software 的更多信息,请访问我们的网站: https://ironsoftware.com/
Iron Software 提供支持
如需一般支持或技术咨询,请发送邮件至: mailto:support@ironsoftware.com/mailto:support@ironsoftware.com
