System.Drawingオブジェクトからバーコードを読む方法

How to Read Barcodes From System.Drawing Objects

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

System.Drawing オブジェクトは、開発者が画像処理に関連するタスクで.NETで広く使用されています。 しかし、マイクロソフトは MacOSLinux などの他のオペレーティングシステムでの System.Drawing のサポートを中止 し、現在はWindows のみをサポートしています。 この大きな変更は、Windows以外のオペレーティングシステムでIronBarcodeを使用する開発者に複数の問題を引き起こしました。 これは、バーコードを扱う際には通常、グラフィックス画像フォント などのオブジェクトを使用する必要があるためです。

この問題に対処するために、代替ソリューションの IronDrawing を導入しました。 この 無料オープンソース のライブラリは、IronSoftwareによって開始され、Windows以外のオペレーティングシステムでの動作を容易にすることを目的としています。 これにより、ユーザーにとって使いやすい体験が提供されます。 NuGet から IronBarcode をインストールすると、自動的に IronDrawing がプロジェクトに含まれます。

クイックスタート: AnyBitmapを使用して1行でバーコードを読む

このスニペットは、IronBarcodeがSystem.Drawing.Bitmapを作成し、IronDrawingがそれをAnyBitmapに暗黙的にキャストしてどれほど簡単にバーコードを読むことができるかを示しています。たった1行で、どのOSの開発者でもすばやく簡単に結果を得ることができます。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小ワークフロー(5ステップ)

  1. System.Drawing からのバーコードを読むためのC#ライブラリをダウンロード
  2. IronDrawingを利用してSystem.DrawingオブジェクトをAnyBitmapにキャスト
  3. Read メソッドを使用してAnyBitmapオブジェクトからバーコードを読み取る
  4. 検出されたバーコードの値をコンソールに表示
  5. 別のガイドを参照して、IronDrawingがカラーとフォントの処理に使用される方法を学ぶ

System.DrawingをAnyBitmapにキャストする

System.Drawingからバーコードを読むことは、オブジェクトをAnyBitmapにキャストするという単純なステップを含みます。IronDrawingは使いやすさを考慮して設計されました。 その結果、IronDrawingはSystem.Drawingからの画像オブジェクトをIronSoftware.Drawing画像オブジェクトであるAnyBitmapに暗黙的にキャストすることをサポートしています。

System.Drawingオブジェクトに加えて、他のタイプのオブジェクトからのキャストもサポートしています。これには以下が含まれます:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SkiaSharp.SKImage
  • SixLabors.ImageSharp

利用者は上記のオブジェクトのキャストの< a href="/open-source/csharp/drawing/examples/cast-to-anybitmap/">コード例を参照することができます。 以下は、System.DrawingオブジェクトからIronSoftware.Drawing.AnyBitmapへバーコードの画像をキャストする方法を示すコードスニペットです。 ここに簡単な例があります。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.cs
using IronSoftware.Drawing;
using System.Collections.Generic;

List<AnyBitmap> barcodes = new List<AnyBitmap>();

// Instantiate System.Drawing.Bitmap
System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");

// Cast from System.Drawing.Bitmap to AnyBitmap
AnyBitmap barcode1 = bitmapFromBitmap;

barcodes.Add(barcode1);

// Instantiate System.Drawing.Bitmap
System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");

// Cast from System.Drawing.Image to AnyBitmap
AnyBitmap barcode2 = bitmapFromFile;

barcodes.Add(barcode2);
Imports IronSoftware.Drawing
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

' Instantiate System.Drawing.Bitmap
Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")

' Cast from System.Drawing.Bitmap to AnyBitmap
Private barcode1 As AnyBitmap = bitmapFromBitmap

barcodes.Add(barcode1)

' Instantiate System.Drawing.Bitmap
Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")

' Cast from System.Drawing.Image to AnyBitmap
Dim barcode2 As AnyBitmap = bitmapFromFile

barcodes.Add(barcode2)
$vbLabelText   $csharpLabel

上記のコードスニペットでは、2つのバーコード画像をSystem.Drawing.BitmapSystem.Drawing.Imageとして読み込みました。 次に、それらをAnyBitmapオブジェクトに割り当てるだけで暗黙的にキャストしました。 その後、これらのオブジェクトをAnyBitmapリストに追加しました。

AnyBitmapからバーコードを読む

IronBarcodeは、追加の構成を必要とせず、すべてのメソッドでIronSoftware.Drawing.AnyBitmapオブジェクトを容易に受け入れることができます。 これは、Windows以外のオペレーティングシステムがサポートされていないSystem.Drawingオブジェクトを使用する開発者に使いやすさを提供します。 以下のコードスニペットはこれを実行する方法を示しています。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;

List<AnyBitmap> barcodes = new List<AnyBitmap>();

System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");
AnyBitmap barcode1 = bitmapFromBitmap;
barcodes.Add(barcode1);

System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");
AnyBitmap barcode2 = bitmapFromFile;
barcodes.Add(barcode2);

foreach (var barcode in barcodes)
{
    // Read the barcode
    var results = BarcodeReader.Read(barcode);
    foreach (var result in results)
    {
        // Output the detected barcode value
        Console.WriteLine(result.Value);
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")
Private barcode1 As AnyBitmap = bitmapFromBitmap
barcodes.Add(barcode1)

Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")
Dim barcode2 As AnyBitmap = bitmapFromFile
barcodes.Add(barcode2)

For Each barcode In barcodes
	' Read the barcode
	Dim results = BarcodeReader.Read(barcode)
	For Each result In results
		' Output the detected barcode value
		Console.WriteLine(result.Value)
	Next result
Next barcode
$vbLabelText   $csharpLabel

上記のコードスニペットは、前のものの拡張です。 一度AnyBitmapリストを生成した後、リストを反復し、各AnyBitmapオブジェクトをパラメーターとしてReadメソッドを呼び出し、その後IronBarcode.BarcodeResultsを返しました。 その後、返されたオブジェクトを反復し、バーコードの値をコンソールに印刷しました。

IronSoftware.Drawingの機能領域は、画像のキャストに限定されていません。 また、バーコードやQRコードのスタイリングに役立つフォントなどの他の画像処理要素にも大いに使用されています。 利用者は、IronDrawingを使用してQRコードにカスタマイズやロゴを追加する方法を探究することができます</a。

よくある質問

どうすれば .NET C# で System.Drawing オブジェクトからバーコードを読むことができますか?

IronBarcode と IronDrawing を組み合わせて使用することで、System.Drawing オブジェクトからバーコードを読むことができます。最初に IronDrawing を使って System.Drawing オブジェクトを AnyBitmap にキャストし、次に IronBarcode の Read メソッドを使用してバーコードを読み取ります。

IronDrawing とは何ですか、そしてバーコード読み取りにどのように役立ちますか?

IronDrawing は IronSoftware による無料かつオープンソースのライブラリで、System.Drawing オブジェクトを AnyBitmap へ暗黙的にキャストすることを可能にします。これにより、IronBarcode を使用する際に非 Windows オペレーティングシステム上でのバーコード読取が可能になります。

MacOS と Linux で IronBarcode を使ってバーコードを読むことができますか?

はい、IronDrawing を使用することで System.Drawing オブジェクトを AnyBitmap にキャストし、MacOS と Linux 上で IronBarcode によるバーコード読み取りが可能になります。これにより、System.Drawing が Windows のみに対応しているという制限を克服します。

バーコード読み取りのために AnyBitmap にキャストできる画像オブジェクトの種類は何ですか?

System.Drawing オブジェクト以外にも、System.Drawing.Bitmap、System.Drawing.Image、SkiaSharp.SKBitmap、SkiaSharp.SKImage、および SixLabors.ImageSharp オブジェクトを AnyBitmap にキャストして、IronBarcode でバーコードを読み取ることができます。

IronBarcode を使って検出されたバーコード値をどのように表示するのですか?

IronBarcode の Read メソッドを使用してバーコードを読み取った後、BarcodeResult 配列を反復し、各バーコード値をコンソールに出力します。

NuGet からバーコード読み取りライブラリをインストールする際に IronDrawing が含まれていますか?

はい、IronBarcode を NuGet からインストールする際に IronDrawing はプロジェクトに自動的に含まれ、バーコード読み取りのためのシームレスな統合を提供します。

画像オブジェクトの暗黙的キャストがバーコード処理にどのように役立つか?

IronDrawing を利用して System.Drawing オブジェクトを AnyBitmap に暗黙的にキャストすることは、これらを IronBarcode に対応させるプロセスを簡素化し、様々なオペレーティングシステム上でのバーコード処理を強化します。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、およびJavaの知識を磨いており、その知識を利用してIron Software全体のチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はいいですか?
Nuget ダウンロード 1,935,276 | バージョン: 2025.11 ただ今リリースされました