How to Read Barcodes From System.Drawing in C
IronBarcodeは、すべてのオペレーティングシステム上でSystem.Drawingのサポートに関するMicrosoftのWindows限定という制限を解決します。
はじめに
System.Drawing オブジェクトは、.NET における画像処理タスクで広く使用されています。 ただし、MicrosoftはmacOSおよびLinuxにおけるSystem.Drawingのサポートを終了し、現在はWindowsのみをサポートしています。 この変更により、Windows以外のオペレーティングシステムでIronBarcodeを使用している開発者に問題が生じています。バーコードの処理には通常、グラフィックス、画像、およびフォントが関わるためです。
この問題に対処するため、IronDrawingを導入しました。 Iron Softwareが開発したこの無料のオープンソースライブラリは、クロスプラットフォーム対応を簡素化し、シームレスな体験を提供します。 NuGetからIronBarcodeをインストールすると、IronDrawingがプロジェクトに自動的に含まれます。
BarCode読み取りが初めての開発者の方は、基礎概念や基本的な使用パターンを網羅した"BarCode読み取りチュートリアル"をご覧ください。 さまざまな画像形式を扱う場合は、画像からBARCODEを読み取るためのガイドで、追加の背景情報や例をご確認いただけます。
クイックスタート:たった1行のコードで AnyBitmap を使用してBARCODEを読み取る
このスニペットは、System.Drawing.Bitmap を作成し、IronDrawing によってそれを AnyBitmap に暗黙的にキャストさせることで、IronBarcode が BARCODE を読み取る方法を示しています。 たった1行のコードで、どのOSのデベロッパーでも素早く結果を得られます。
最小限のワークフロー(5ステップ)
- BARCODE を読み取るための C# ライブラリを以下からダウンロードしてください
System.Drawing - "cast"という動詞を使用し
IronDrawingを使用してSystem.Drawingを使用してAnyBitmap -
Readメソッドを使用して、BARCODEを読み取る方法AnyBitmapオブジェクトから読み取る - 検出したBARCODEの値をコンソールに表示する
- 別の記事を読んで、
IronDrawingが色やフォントの処理にどのように使用されるか
System.Drawing オブジェクトを AnyBitmap にキャストするにはどうすればよいですか?
AnyBitmapにキャストする必要があります。 IronDrawing は使いやすさを重視して設計されており、System.Drawing からの画像オブジェクトを、IronSoftware.Drawing と呼ばれる AnyBitmap 画像オブジェクトへの暗黙的なキャストをサポートしています。
System.Drawing オブジェクト以外にも、他の型からのキャストをサポートしています:
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharp
上記のオブジェクトのキャストについては、このコード例を参照してください。 以下は、System.Drawing オブジェクトから IronSoftware.Drawing.AnyBitmap への BARCODE 画像のキャスト例です:
どの 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)
このコードは、System.Drawing オブジェクトと IronBarcode との間で、IronDrawing を通じてシームレスに連携する様子を示しています。 この互換性は、QRコード、Code 128、Code 39など、当社の"対応BARCODE形式ガイド"に詳述されているさまざまなBARCODE形式に及びます。
暗黙の型変換はなぜ機能するのか?
上記のコードでは、2つのBARCODE画像を System.Drawing.Bitmap および System.Drawing.Image として読み込みました。 次に、それらを AnyBitmap オブジェクトに代入することで暗黙的に AnyBitmap に変換し、これらのオブジェクトを AnyBitmap リストに追加しました。
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
よくある型変換エラーとは?
System.Drawing を AnyBitmap に変換する際、開発者は次のような状況に遭遇する可能性があります:
- Null Reference Exceptions: キャストを行う前に、
System.Drawingオブジェクトが null ではないことを確認してください - 非対応フォーマットによる例外: 一部の特殊な画像フォーマットでは、事前変換が必要となります
- メモリの問題:大きな画像には適切な破棄パターンが必要です
型変換に関するトラブルシューティングについては、当社のトラブルシューティングガイドで、BarCode認識時に発生する一般的な問題の解決策をご案内しています。
AnyBitmap オブジェクトから BARCODE を読み取るにはどうすればよいですか?
IronBarcodeは、追加の設定なしにすべてのメソッドでIronSoftware.Drawing.AnyBitmapオブジェクトを受け入れます。 これにより、Windows以外のオペレーティングシステム上でSystem.Drawingオブジェクトを使用する際の開発が簡素化されます。 以下のコードがその例です:
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;
// Create a list of image file paths to read barcodes from
List<string> barcodeFiles = new List<string>
{
"test1.jpg",
"test2.png"
};
foreach (var barcodeFile in barcodeFiles)
{
// Read the barcode from file path
var results = BarcodeReader.Read(barcodeFile);
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
' Create a list of image file paths to read barcodes from
Dim barcodeFiles As New List(Of String) From {
"test1.jpg",
"test2.png"
}
For Each barcodeFile In barcodeFiles
' Read the barcode from file path
Dim results = BarcodeReader.Read(barcodeFile)
For Each result In results
' Output the detected barcode value
Console.WriteLine(result.Value)
Next
Next
基本的な Read メソッドに加え、IronBarcode には AnyBitmap パラメータを受け取るいくつかのメソッドが用意されています。 より高度なシナリオについては、1つの画像内の複数のBarCodeを効率的に処理する方法を解説した"複数のBarCodeの読み取り"ガイドをご覧ください:
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Co/de128,
// 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.Co/de128,
// 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)
複数のBarCode検索結果にはどのように対処すればよいですか?
上記のコードは、前の例を拡張したものです。 AnyBitmap リストにデータを入力した後、そのリストを順に処理し、各 AnyBitmap オブジェクトに対して Read メソッドを呼び出したところ、IronBarCode.BarcodeResults が返されました。 その後、結果を順次処理して、BARCODEの値をコンソールにPRINTしました。
複数の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
他にどのような IronDrawing 機能を利用できますか?
IronSoftware.Drawing の機能は、画像のキャスティングだけにとどまりません。 BARCODEやQRコードのスタイル設定に役立つ、色やフォントなどの画像処理機能に対応しています。 IronDrawing を活用して、QRコードをカスタマイズしたりロゴを追加したりする方法をご覧ください。
IronDrawing は、BarCode 処理を補完する強力な画像操作機能を提供します:
// 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.Co/ntrast(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.Co/ntrast(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)
特定の画像補正が必要な場合については、BARCODEの読み取りやすさを向上させるためのフィルターの使用方法について、当社の画像補正ガイドに詳しく記載されています。
なぜ IronDrawing ではなく System.Drawing を選ぶべきなのでしょうか?
System.Drawingに比べて次のような大きな利点があります:
- クロスプラットフォーム対応:
System.Drawing(.NET Core/5+ では Windows のみ)とは異なり、Windows、Linux、macOS でシームレスに動作します。 - 最新のアーキテクチャ:パフォーマンスとメモリ管理の向上を図るため、
SkiaSharpおよびImageSharpを基盤として構築されています - 簡素化されたAPI:おなじみの
System.Drawingのようなインターフェースを維持しつつ、現代的な利便性を追加 - アクティブな開発: メンテナンスモードにある
System.Drawingとは異なり、定期的な更新と改善が行われています - より優れた統合性:Iron Software製品との最適なパフォーマンスを実現するために特別に設計されています
デプロイに関する考慮事項、特にクラウド環境については、IronDrawingを使用したクロスプラットフォーム互換性に関する具体的な注意事項を含む、AzureへのデプロイおよびAWSへのデプロイに関するガイドを参照してください。
デスクトップアプリケーション、Webサービス、クラウドネイティブソリューションのいずれを構築する場合でも、IronDrawing を使用すれば、BARCODE処理コードの移植性と効率性をすべてのプラットフォームで維持できるため、最新の .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にキャストし、画像に存在する全てのバーコードを読み取ります。
IronBarcodeはバーコードの外観カスタマイズをサポートしていますか?
はい、IronBarcodeはカラー、サイズ、テキスト注釈を含むバーコードの外観に関する詳細なカスタマイズオプションを提供し、特定のデザイン要件に合わせて調整が可能です。
IronBarcodeはビジネスプロセスの効率向上にどのように役立ちますか?
IronBarcodeは迅速かつ正確なバーコード生成と読み取りを可能にし、手動データ入力エラーの減少、在庫および資産追跡の改善などにより、ビジネスプロセスの効率を向上させます。
プロジェクトにIronBarcodeを実装するために必要なプログラミングスキルは何ですか?
IronBarcodeをプロジェクトに実装するためには、C#プログラミングの基本的な知識があれば十分で、開発者をガイドするための簡単なメソッドと包括的なドキュメントが提供されています。
IronBarcodeは小規模プロジェクトと大規模エンタープライズアプリケーションの両方に適していますか?
IronBarcodeはスケーラブルかつ多用途に設計されており、小規模プロジェクトおよび強力なバーコードソリューションを必要とする大規模エンタープライズアプリケーションの両方に適しています。

