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

C&numのSystem.DrawingからBarCodeを読み取る方法。

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

IronBarcodeは、IronDrawingを通じてAnyBitmapに自動的に変換することで、すべてのオペレーティングシステム上のSystem.Drawingオブジェクトからバーコードを読み取ることを可能にし、System.Drawingサポートに関するマイクロソフトのWindowsのみの制限を解決します。

はじめに

. 。 <!コードの概念を示す図またはスクリーンショット -->。

System.Drawingオブジェクトは、.NETで画像処理タスクに広く使用されています。 しかし、Microsoft は MacOSLinux での System.Drawing のサポートを終了し、現在は Windows のみをサポートしています。 この変更により、非WindowsオペレーティングシステムでIronBarcodeを使用する開発者に問題が生じました。なぜなら、バーコードの作業には通常グラフィックス画像フォントが含まれるからです。

この問題に対処するため、IronDrawingを導入しました。 IronSoftwareによって作成されたこの無料かつオープンソースライブラリは、クロスプラットフォームのサポートを簡素化し、シームレスなエクスペリエンスを提供します。 NuGetからIronBarcodeをインストールすると、IronDrawingが自動的にプロジェクトに含まれます。

バーコード読み取りが初めての開発者には、基本的な概念と基本的な使用パターンを網羅した包括的な Reading Barcodes Tutorial を参照してください。 さまざまな画像フォーマットで作業している場合は、画像から BarCode を読み取るガイドに、追加のコンテキストと例が記載されています。

クイックスタート: AnyBitmapを使ったバーコードの読み取りを1行で簡単に

このスニペットは、System.Drawing.Bitmapを作成し、IronDrawingに暗黙的にAnyBitmapにキャストさせることで、IronBarcodeがどのようにバーコードを読み取るかを示しています。 たった1行で、どのOSの開発者でも迅速に結果を得ることができます。

Nuget Icon今すぐ NuGet で PDF を作成してみましょう:

  1. NuGet パッケージ マネージャーを使用して IronBarcode をインストールします

    PM > Install-Package BarCode

  2. このコード スニペットをコピーして実行します。

    var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
  3. 実際の環境でテストするためにデプロイする

    今すぐ無料トライアルでプロジェクトに IronBarcode を使い始めましょう
    arrow pointer

どのように System.Drawing オブジェクトを AnyBitmap にキャストしますか?

. 。 <!コードの概念を示す図またはスクリーンショット -->。

System.Drawing からバーコードを読み取るには、オブジェクトを AnyBitmap にキャストする必要があります。 IronDrawingは使いやすく設計されており、System.DrawingからAnyBitmapと呼ばれるIronSoftware.Drawing画像オブジェクトへの暗黙のキャストをサポートしています。

System.Drawingオブジェクト以外にも、他の型からのキャストをサポートしています:

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

上記のオブジェクトをキャストするには、このコード例を参照してください。 下記は、System.DrawingオブジェクトからIronSoftware.Drawing.AnyBitmapへのバーコード画像のキャストを示しています:

どの System.Drawing 型をキャストできますか?

: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

このコードは、IronDrawingを通して、System.DrawingオブジェクトとIronBarcode間のシームレスな統合を示しています。 この互換性は、サポートされているバーコード形式ガイドに詳述されている、QRコード、Code 128、Code 39、その他多くのものを含む、さまざまなバーコード形式にわたって拡張されます。

なぜ暗黙的キャスティングは機能するのですか?

上記のコードでは、2つのバーコード画像をSystem.Drawing.BitmapSystem.Drawing.Imageとしてロードしています。 次に、それらを AnyBitmap オブジェクトに代入することによって、暗黙的に AnyBitmap にキャストし、これらのオブジェクトを AnyBitmap リストに追加しました。

IronDrawingの暗黙のキャスト機構は演算子のオーバーロードを使用し、System.Drawing型とAnyBitmap間の透過的な変換を提供します。 このデザインパターンにより、開発者は既存のコードを維持しながら、クロスプラットフォームの互換性を得ることができます。 変換では、解像度、色深度、ピクセルデータを含むすべての画像プロパティが保持され、品質が損なわれることはありません。

明示的キャストと暗黙的キャストはいつ使い分けるべきですか?

暗黙的なキャストは利便性を提供しますが、シナリオによっては明示的なキャストが望ましい場合もあります:

// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast

// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast

// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
    AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
    // Process the barcode
}
// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast

// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast

// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
    AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
    // Process the barcode
}
Imports System.Drawing

' Implicit casting - clean and simple for straightforward conversions
Dim systemBitmap As New Bitmap("barcode.png")
Dim anyBitmap As AnyBitmap = systemBitmap ' Implicit cast

' Explicit casting - useful when type clarity is important
Dim systemImage As Image = Image.FromFile("qrcode.jpg")
Dim explicitBitmap As AnyBitmap = CType(systemImage, AnyBitmap) ' Explicit cast

' When working with nullable types or conditional logic
Dim nullableBitmap As Bitmap = GetBitmapFromSource()
If nullableBitmap IsNot Nothing Then
    Dim result As AnyBitmap = CType(nullableBitmap, AnyBitmap) ' Explicit cast for clarity
    ' Process the barcode
End If
$vbLabelText   $csharpLabel

よくある鋳造エラーとは

System.DrawingAnyBitmapに変換するとき、開発者は以下のことに遭遇するかもしれません:

1.ヌル参照例外:キャストする前に、System.Drawing オブジェクトが NULL でないことを確認してください。 2.サポートされていないフォーマットの例外: エキゾチックな画像フォーマットの中には、事前に変換が必要なものがあります。 3.メモリの問題:大きな画像は適切な処理パターンが必要

鋳造問題のトラブルシューティングについては、トラブルシューティング ガイドでバーコード認識時の一般的な問題の解決策を提供しています。

AnyBitmapオブジェクトから BarCode を読み取るには?

. 。 <!コードの概念を示す図またはスクリーンショット -->。

IronBarcodeはIronSoftware.Drawing.AnyBitmapオブジェクトを追加設定なしですべてのメソッドで受け入れます。 これは、System.Drawing オブジェクトを Windows 以外のオペレーティング システムで使用する際の開発を簡素化します。 次のコードはこれを示しています:

どのメソッドが AnyBitmap パラメータを受け付けますか?

: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

基本的なReadメソッド以外にも、IronBarcodeはAnyBitmapパラメータを受け付けるメソッドをいくつか提供しています。 高度なシナリオについては、複数の BarCode を読み取るガイドで、1 つの画像内の複数のバーコードの効率的な処理を実演しています:

// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
    // Specify barcode types to search for
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // Enable machine learning for better accuracy
    UseML = true,
    // Set confidence threshold
    Confidence = 0.95
};

// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
    // Specify barcode types to search for
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // Enable machine learning for better accuracy
    UseML = true,
    // Set confidence threshold
    Confidence = 0.95
};

// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
' Advanced barcode reading with options
Dim readerOptions As New BarcodeReaderOptions With {
    ' Specify barcode types to search for
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
    ' Enable machine learning for better accuracy
    .UseML = True,
    ' Set confidence threshold
    .Confidence = 0.95
}

' Read with specific options
Dim advancedResults = BarcodeReader.Read(anyBitmap, readerOptions)
$vbLabelText   $csharpLabel

複数の BarCode 結果を処理するにはどうすればよいですか?

上のコードは、前の例を拡張したものです。 AnyBitmapリストを入力した後、それを繰り返し、各AnyBitmapオブジェクトのReadメソッドを呼び出し、IronBarcode.BarcodeResultsを返しました。 次に、バーコードの値をコンソールに出力するために、結果を反復しました。

複数の BarCode を処理する場合は、並列処理を活用してパフォーマンスを向上させます:

// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();

Parallel.ForEach(barcodeFiles, file =>
{
    var bitmap = new System.Drawing.Bitmap(file);
    var anyBitmap = (AnyBitmap)bitmap;
    var results = BarcodeReader.Read(anyBitmap);

    foreach (var result in results)
    {
        allResults.Add(result);
    }

    bitmap.Dispose(); // Clean up resources
});

// Process all results
foreach (var result in allResults)
{
    Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}
// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();

Parallel.ForEach(barcodeFiles, file =>
{
    var bitmap = new System.Drawing.Bitmap(file);
    var anyBitmap = (AnyBitmap)bitmap;
    var results = BarcodeReader.Read(anyBitmap);

    foreach (var result in results)
    {
        allResults.Add(result);
    }

    bitmap.Dispose(); // Clean up resources
});

// Process all results
foreach (var result in allResults)
{
    Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}
Imports System.IO
Imports System.Collections.Concurrent
Imports System.Drawing
Imports System.Threading.Tasks

' Parallel processing for multiple barcode images
Dim barcodeFiles = Directory.GetFiles("barcodes/", "*.png")
Dim allResults = New ConcurrentBag(Of BarcodeResult)()

Parallel.ForEach(barcodeFiles, Sub(file)
                                   Dim bitmap = New Bitmap(file)
                                   Dim anyBitmap = CType(bitmap, AnyBitmap)
                                   Dim results = BarcodeReader.Read(anyBitmap)

                                   For Each result In results
                                       allResults.Add(result)
                                   Next

                                   bitmap.Dispose() ' Clean up resources
                               End Sub)

' Process all results
For Each result In allResults
    Console.WriteLine($"Found {result.BarcodeType}: {result.Value}")
Next
$vbLabelText   $csharpLabel

他にどのようなIronDrawing機能が使えますか?

IronSoftware.Drawingの機能は、画像のキャストにとどまりません。 バーコードやQRコードのスタイリングに役立つフォントのような画像処理の側面を扱います。 QRコードにロゴをカスタマイズして追加するために、IronDrawingをどのように活用しているかをご覧ください。

IronDrawingは、バーコード処理を補完する強力な画像操作機能を提供します:

// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;

// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");

// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image

// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;

// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");

// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image

// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
Imports IronSoftware.Drawing

' Load and preprocess an image before barcode reading
Dim preprocessedImage As AnyBitmap = AnyBitmap.FromFile("noisy-barcode.jpg")

' Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale()
preprocessedImage = preprocessedImage.Contrast(1.5) ' Increase contrast
preprocessedImage = preprocessedImage.Sharpen() ' Sharpen image

' Read the preprocessed barcode
Dim improvedResults = BarcodeReader.Read(preprocessedImage)
$vbLabelText   $csharpLabel

特定の画像補正が必要なシナリオについては、画像補正ガイドで、バーコードの可読性を高めるフィルタの使用方法について詳しく説明しています。

System.DrawingよりもIronDrawingを選ぶ理由?

IronDrawingは、System.Drawingと比較して魅力的な利点を提供します:

1.クロスプラットフォームのサポートSystem.Drawing(.NET Core/5+ではWindowsのみ)とは異なり、Windows、Linux、macOSでシームレスに動作します。 2.最新のアーキテクチャSkiaSharpImageSharpをベースに構築されており、パフォーマンスとメモリ管理が向上しています。 3.簡略化されたAPI:使い慣れたSystem.Drawingのようなインターフェイスを維持しながら、現代的な利便性を追加します。 4.積極的な開発:メンテナンスモードのSystem.Drawingとは異なり、定期的な更新と改善 5.より優れた統合:ソフトウェア製品との最適なパフォーマンスのために特別に設計されています。

特にクラウド環境へのデプロイについては、AzureへのデプロイAWSへのデプロイのガイドを参照してください。AWSへのデプロイには、IronDrawingを使用したクロスプラットフォームの互換性に関する具体的な注意事項が含まれています。

デスクトップ アプリケーション、Web サービス、クラウド ネイティブ ソリューションのいずれを構築する場合でも、IronDrawingは、バーコード処理コードがすべてのプラットフォームで移植性と効率性を維持することを保証し、最新の .NET 開発に理想的な選択肢となります。

よくある質問

Windows 以外のプラットフォームで System.Drawing オブジェクトから BarCode を読み取る方法を教えてください。

IronBarcodeはIronDrawingを通してSystem.Drawingオブジェクトからのクロスプラットフォームのバーコード読み取りを自動的に処理し、AnyBitmapフォーマットに変換します。これはSystem.DrawingがWindows専用であるというマイクロソフト社の制限を解決し、MacOSやLinuxシステムでシームレスにバーコードを読み取ることを可能にします。

IronDrawingとは何ですか?なぜバーコード読み取りと一緒に含まれているのですか?

IronDrawingはIron Softwareによって作成された無料のオープンソースライブラリで、グラフィックス操作をクロスプラットフォームでサポートします。NuGetからIronBarcodeをインストールすると自動的に含まれ、互換性のあるAnyBitmapフォーマットに変換することで、すべてのオペレーティングシステムでSystem.Drawingオブジェクトからのバーコード読み取りを可能にします。

System.Drawing.Bitmapからバーコードを読み取るための変換方法を教えてください。

AnyBitmapにキャストするだけで、System.Drawing.BitmapからBarCodeを読み取ることができます:var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`.IronBarcodeはIronDrawingの暗黙のキャスト機能によって自動的に変換を処理します。

LinuxやMacOSでSystem.Drawingを使ってBarCodeを読むことはできますか?

はい、IronBarcodeはIronDrawingを通してLinuxやMacOSのSystem.Drawingオブジェクトからバーコードの読み取りを可能にします。IronDrawingはSystem.DrawingオブジェクトをクロスプラットフォームのAnyBitmapフォーマットに自動的に変換します。これはMicrosoftのSystem.DrawingサポートのWindowsのみの制限を克服しています。

バーコードの読み取りには、どのようなタイプのSystem.Drawingオブジェクトを使用できますか?

IronBarcodeは、System.Drawing.Bitmapやその他の画像タイプを含む様々なSystem.Drawingオブジェクトからのバーコード読み取りをサポートしています。これらはIronDrawingの暗黙のキャスト機能によって自動的にAnyBitmapに変換され、クロスプラットフォームのバーコードスキャン機能を実現します。

System.DrawingからBarCodeを読み取る簡単な1行ソリューションはありますか?

はい、IronBarcodeは1行で解決できます: `var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`.この一行でSystem.Drawing.Bitmapを作成し、IronDrawingを通してAnyBitmapにキャストし、画像に存在する全てのバーコードを読み取ります。

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