.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 パッケージの開発は簡単なはずです。

IronDrawing は、進化するすべての新しい提案された .NET 5、6、7、8 グラフィック標準間のシームレスなブリッジとして機能するため、好みの標準に合わせて開発するだけで済みます。

IronDrawingは無料のオープンソースであり、ほとんどの.NET描画ライブラリ形式をサポートしています。

コンテクスト

Microsoft .NET は、世界中の何百万ものソフトウェア開発者によって使用されている、マルチプラットフォームで互換性の高いソフトウェア フレームワークです。 Microsoft は、 System.Drawing.Commonが Windows プラットフォームでのみサポートされるという重大な変更を発表しました。

これは、 System.Drawing.Commonを使用するクロスプラットフォーム ライブラリを管理する .NET ライブラリ開発者にとって問題となります。Microsoft の推奨アクションでは、Windows 以外のユーザーをサポートするためにライブラリを再構築する必要があるためです。 ボランティアや学者は、提案された新しいグラフィック ライブラリが登場するたびに、それらを使用できるようにすべての NuGet パッケージとクラス ライブラリを再構築する必要があると予想されますが、これにより技術的負債が発生し、最新の .NET の採用が遅れることになります。

結局のところ、.NET を使用する場合は NuGet を使用します。

NuGet パッケージ開発者は、.NET を進化させ、私たち自身で毎日記述すると何千時間もかかるパッケージ済みのコードを提供して、私たち全員に貢献しています。

この重大な変更により、NuGet の開発が遅くなり、.NET 開発者が依存しているコード ライブラリが放棄されたり、廃止されたりする可能性があります。 今すぐ行動する必要があります!

私たちのソリューション

すべての古いグラフィック ライブラリと新しいグラフィック ライブラリ間の変換を行う中間グラフィック フォーマットです。

クラス ライブラリ開発者として、パブリックBitmapColorRectangleFont 、およびSize入力と出力は、すべての新しい標準をサポートする 1 つのタイプのみにすることができます。 内部的には、自分の好きなことを続けることができます。

IronDrawing は .NET Framework 4.62 と下位互換性があり、すべてのバージョンの .NET (.NET 8 を含む) をサポートします。

IronDrawing の開発とリリースでは、 BitmapColorRectangleFontSizeなどの重要なクラスに統一された形式がないという問題に対するオープンソース ソリューションを提供することで、この問題を解決します。 Iron Software.DrawingSystem.DrawingMicrosoft.MauiSkiaSharpSixLaborsのこれらの型の実装間でシームレスに変換します。 これにより、開発者はライブラリ内のこれらのクラスのインスタンスをすべて置き換える必要がなくなります。

たとえばSystem.Drawing.Bitmapを使用している場合は、 System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharpMicrosoft.Maui.Graphics.Platform.PlatformImageのいずれかの型への暗黙的な変換を持つ IronDrawing のAnyBitmapクラスを使用できます。

なぜ無料でこれを行っているのか

私たち Iron Software は、.NET の進化に関心を持ち、.NET の成長と成功を願う、長年のベテラン .NET 開発者であるため、IronDrawing を開発しました。 .NET 7 に見られるように、.NET が成長し進化していくのを見るのは私たちにとって嬉しいことです。私たちは 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
  • Color : 普遍的に互換性のある Color クラス。 Iron Software.Drawing.Colorと次の間の暗黙的なキャストがサポートされています。
    • System.Drawing.Color .色
    • SkiaSharp.SKColor
    • SixLabors.ImageSharp.Color
    • SixLabors.ImageSharp.PixelFormats
  • Rectangle : 普遍的に互換性のある 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 Web サイトから直接ダウンロードすることもできます。

インストールが完了したら、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 のリポジトリにプル リクエストを送信する前にライセンスをお読みください。

情報

Iron Software の詳細については、当社の Web サイトをご覧ください。https://ironsoftware.com/

Iron Software からのサポート

一般的なサポートや技術的なお問い合わせについては、以下のメールアドレスまでご連絡ください。mailto:support@ironsoftware.com

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 13,852,431 | Version: 2025.3 リリース