IronDrawing 开始 用于替代 .NET 中System.Drawing.Common的 C# 位图库 Curtis Chau 已更新:六月 9, 2025 下载 IronDrawing NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 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 图形标准之间的无缝桥梁,随着这些标准的演变,您只需针对您喜欢的标准进行开发即可。 避免出现"此平台不支持 system.drawing.common"异常的技术步骤 Implement System.drawing.common is not possible for .NET 6 and .NET 7 & 8 on Linux and macOS platform 在这些平台上使用 System.Drawing.Common 将会遇到异常消息 将 .NET 6 的System.Drawing.EnableUnixSupport运行时配置设置为true .NET 7 和 8 不再支持 System.Drawing.Common。 使用开源库IronSoftware.Drawing替换 .NET 7 中的System.Drawing.Common。 语境 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等重要类缺乏统一格式的问题提供了一个开源解决方案。 Iron Software.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 开发人员受益。 Iron Software.Drawing功能 AnyBitmap :一个通用的 Bitmap 类。 Iron Software.Drawing.AnyBitmap与以下类型之间支持隐式转换: System.Drawing.Bitmap System.Drawing.Image SkiaSharp.SKBitmap SkiaSharp.SKImage SixLabors.ImageSharp Microsoft.Maui.Graphics.Platform.PlatformImage -颜色:一种通用的颜色类。 Iron Software.Drawing.Color与以下类型之间支持隐式转换: System.Drawing.Color SkiaSharp.SKColor SixLabors.ImageSharp.Color SixLabors.ImageSharp.PixelFormats Rectangle :一个通用的矩形类。 支持Iron Software.Drawing.Rectangle与以下类型之间的隐式转换: System.Drawing.Rectangle SkiaSharp.SKRect SkiaSharp.SKRectI SixLabors.ImageSharp.Rectangle -字体:一种通用兼容的字体类。 支持Iron Software.Drawing.Font与以下类型之间的隐式转换: System.Drawing.Font .字体 SkiaSharp.SKFont SixLabors.Fonts.Font 兼容性 Iron Software.Drawing具有跨平台兼容性,支持以下平台: .NET 8、.NET 7、.NET 6、.NET 5、.NET Core、.NET Standard 和 .NET Framework 4.62+ Windows、macOS、Linux、Docker、Azure 和 AWS 安装 安装 IronDrawing ( Iron Software.Drawing ) NuGet 包既快捷又简单。 请按如下方式安装软件包: Install-Package IronSoftware.System.Drawing 或者,直接从NuGet 官方网站下载。 安装完成后,您可以通过在 C# 代码顶部添加using Iron Software.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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 13,852,431 | Version: 2025.3 刚刚发布 免费 NuGet 下载 总下载量:13,852,431 查看许可证