C# Bitmap Library to Replace System.Drawing.Com/mon in .NET

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronDrawing ist kostenlos, Open-Source und unterstützt die meisten .NET-Zeichenbibliotheksformate

IronDrawing ist eine ursprünglich von Iron Software entwickelte Open-Source-Bibliothek, die C#-Softwareentwicklern hilft, System.Drawing.Com/mon in .NET-Projekten auf Windows-, macOS- und Linux-Plattformen zu ersetzen.

Die Entwicklung von Klassenbibliotheken und NuGet-Paketen for .NET 5, 6, 7 und 8, die Grafiken, Bilder und Schriftarten unterstützen, sollte einfach sein.

IronDrawing fungiert als nahtlose Brücke zwischen allen neuen vorgeschlagenen .NET 5-, 6-, 7- und 8-Grafikstandards, während diese sich weiterentwickeln – so müssen Sie nur für den Standard entwickeln, den Sie bevorzugen.

IronDrawing ist kostenlos, Open Source und unterstützt die meisten .NET-Zeichenbibliotheksformate

Kontext

Microsoft .NET Framework ist ein plattformübergreifendes, hochkompatibles Software-Framework, das von Millionen von Softwareentwicklern weltweit genutzt wird. Microsoft hat eine grundlegende Änderung angekündigt, wonach System.Drawing.Com/mon nur noch auf Windows-Plattformen unterstützt wird.

Dies ist problematisch für Entwickler von .NET-Bibliotheken, die plattformübergreifende Bibliotheken pflegen, die System.Drawing.Com/mon verwenden, da die von Microsoft empfohlene Vorgehensweise erfordert, dass Sie Ihre Bibliothek neu kompilieren, um Nicht-Windows-Benutzer zu unterstützen. Von Freiwilligen und Wissenschaftlern wird erwartet, dass sie jedes NuGet-Paket und jede Klassenbibliothek neu erstellen, um die jeweils neuen vorgeschlagenen Grafikbibliotheken zu nutzen, sobald diese erscheinen. Dies verursacht technische Schulden, die die Einführung von modernem .NET verlangsamen werden.

Letztendlich verwenden wir, wenn wir .NET nutzen, auch NuGet.

NuGet-Paketentwickler tun uns allen einen Gefallen, indem sie .NET und vorgefertigten Code weiterentwickeln, dessen Erstellung uns selbst täglich Tausende von Stunden kosten würde.

Diese grundlegende Änderung wird die NuGet-Entwicklung verlangsamen und könnte dazu führen, dass Code-Bibliotheken, auf die .NET-Entwickler angewiesen sind, aufgegeben werden oder veralten. Wir müssen jetzt handeln!

Unsere Lösung

Ein zwischengeschaltetes Grafikformat, das zwischen allen alten und neuen Grafikbibliotheken konvertiert.

Als Entwickler von Klassenbibliotheken können Ihre öffentlichen Bitmap, Color, Rectangle, Font und Size Ein- und Ausgänge nur von einem Typ sein, der alle neuen Standards unterstützt. Intern kannst du weiterhin tun, was du möchtest.

IronDrawing ist abwärtskompatibel mit .NET Framework 4.62 und unterstützt alle Versionen von .NET (einschließlich .NET 8).

Die Entwicklung und Veröffentlichung von IronDrawing löst dieses Problem, indem es eine Open-Source-Lösung für das Fehlen eines einheitlichen Formats für wichtige Klassen wie Bitmap, Color, Rectangle, Font und Size. IronSoftware.Drawing konvertiert für Sie nahtlos zwischen den Implementierungen dieser Typen in System.Drawing, Microsoft.Maui, SkiaSharp und SixLabors. Dadurch müssen Sie als Entwickler nicht alle Instanzen dieser Klassen in Ihrer Bibliothek ersetzen.

Wenn Sie beispielsweise System.Drawing.Bitmap verwenden, können Sie die AnyBitmap-Klasse von IronDrawing nutzen, die implizite Konvertierungen in jeden dieser Typen bietet: System.Drawing.Bitmap, System.Drawing.Image, SkiaSharp.SKBitmap, SkiaSharp.SKImage, SixLabors.ImageSharp, Microsoft.Maui.Graphics.Platform.PlatformImage.

Warum wir dies kostenlos tun

Wir haben IronDrawing entwickelt, weil wir bei Iron Software langjährige, erfahrene .NET-Entwickler sind, denen die Weiterentwicklung von .NET am Herzen liegt und die wollen, dass es wächst und erfolgreich ist. Wir freuen uns sehr, dass .NET wächst und sich weiterentwickelt, wie man an .NET 7 sehen kann. Wir nutzen NuGet den ganzen Tag, und ich bin mir sicher, dass Sie das auch tun. Wir unterstützen und hoffen, einen reibungslosen Übergang in die Zukunft und weg von System.Drawing zu fördern.

Wir möchten, dass die Entwicklung von .NET-Klassenbibliotheken und NuGet einfacher wird, damit das NuGet-Ökosystem weiter floriert und alle .NET-Entwickler davon profitieren.

IronSoftware.Drawing Funktionen

  • AnyBitmap: Eine universell kompatible Bitmap-Klasse. Implizites Casting zwischen IronSoftware.Drawing.AnyBitmap und den folgenden wird unterstützt:
    • System.Drawing.Bitmap
    • System.Drawing.Image
    • SkiaSharp.SKBitmap
    • SkiaSharp.SKImage
    • SixLabors.ImageSharp
    • Microsoft.Maui.Graphics.Platform.PlatformImage
  • Color: Eine universell kompatible Color-Klasse. Implizites Casting zwischen IronSoftware.Drawing.Co/lor und den folgenden wird unterstützt:
    • System.Drawing.Co/lor
    • SkiaSharp.SKColor
    • SixLabors.ImageSharp.Co/lor
    • SixLabors.ImageSharp.PixelFormats
  • Rectangle: Eine universell kompatible Rectangle-Klasse. Implizites Casting zwischen IronSoftware.Drawing.Rectangle und den folgenden wird unterstützt:
    • System.Drawing.Rectangle
    • SkiaSharp.SKRect
    • SkiaSharp.SKRectI
    • SixLabors.ImageSharp.Rectangle
  • Font: Eine universell kompatible Font-Klasse. Implizites Casting zwischen IronSoftware.Drawing.Font und den folgenden wird unterstützt:
    • System.Drawing.Font
    • SkiaSharp.SKFont
    • SixLabors.Fonts.Font

Kompatibilität

IronSoftware.Drawing bietet plattformübergreifende Kompatibilität mit:

  • .NET 8, .NET 7, .NET 6, .NET 5, .NET Core, .NET Standard und .NET Framework 4.62+
  • Windows, macOS, Linux, Docker, Azure und AWS

Installation

Die Installation des IronDrawing (IronSoftware.Drawing) NuGet-Pakets ist schnell und einfach. Bitte installieren Sie das Paket wie folgt:

Install-Package IronSoftware.System.Drawing

Alternativ können Sie die Bibliothek direkt von der offiziellen NuGet-Website herunterladen.

Nach der Installation können Sie loslegen, indem Sie using IronSoftware.Drawing; am Anfang Ihres C#-Codes einfügen.

Code-Beispiele

AnyBitmap-Beispiel

: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

Farbbeispiel

: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

Beispiel für ein Rechteck

: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

Schriftart-Beispiel

: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

Verfügbarer Support

Lizenz

Die Lizenzinformationen finden Sie hier: LICENSE.txt

Mitwirken

Wenn Sie zum Open-Source-Projekt IronDrawing beitragen möchten, lesen Sie bitte die Lizenz, bevor Sie einen Pull-Request an das Repository auf GitHub senden.

Informationen

Weitere Informationen zu Iron Software finden Sie auf unserer Website: https://ironsoftware.com/

Unterstützung durch Iron Software

Für allgemeinen Support und technische Anfragen senden Sie uns bitte eine E-Mail an: mailto:support@ironsoftware.com/mailto:support@ironsoftware.com

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 16,991,182 | Version: 2025.3 just released
Still Scrolling Icon

Scrollst du immer noch?

Sie brauchen schnell einen Beweis? PM > Install-Package IronSoftware.System.Drawing
Führen Sie eine Probe aus Sehen Sie zu, wie Ihr HTML-Code in eine PDF-Datei umgewandelt wird.