How to Read Barcodes From System.Drawing in C
IronBarcode は、すべてのオペレーティング システム上の System.Drawing オブジェクトからのバーコードを AnyBitmap から IronDrawing に自動的に変換することで読み取りを可能にし、System.Drawing サポートに関する Microsoft Windows のみの制限を解決します。
はじめに
System.Drawing オブジェクトは、 .NETで画像処理タスクに広く使用されています。 ただし、Microsoft はMacOSおよびLinuxでのSystem.Drawing のサポートを中止し、現在はWindowsのみをサポートしています。 この変更により、非WindowsオペレーティングシステムでIronBarcodeを使用する開発者に問題が生じました。なぜなら、バーコードの作業には通常グラフィックス、画像、フォントが含まれるからです。
この問題に対処するため、IronDrawingを導入しました。 IronSoftwareによって作成されたこの無料かつオープンソースライブラリは、クロスプラットフォームのサポートを簡素化し、シームレスなエクスペリエンスを提供します。 NuGetからIronBarcodeをインストールすると、 IronDrawing がプロジェクトに自動的に含まれます。
バーコード読み取りが初めての開発者には、基本的な概念と基本的な使用パターンを網羅した包括的な Reading Barcodes Tutorial を参照してください。 さまざまな画像フォーマットで作業している場合は、画像から BarCode を読み取るガイドに、追加のコンテキストと例が記載されています。
クイックスタート: AnyBitmap を使用してバーコードを 1 行で簡単に読み取ります
このスニペットは、System.Drawing.Bitmap を作成し、IronDrawing がそれを暗黙的に AnyBitmap にキャストすることで、 IronBarcode がバーコードを読み取る方法を示しています。 たった1行で、どのOSの開発者でも迅速に結果を得ることができます。
最小限のワークフロー(5ステップ)
- `System.Drawing`からバーコードを読み取るためのC#ライブラリをダウンロードします。
- Utilize `IronDrawing` to cast `System.Drawing` objects into `AnyBitmap`
- Use the `Read` method to read barcodes from `AnyBitmap` objects
- コンソールに検出されたバーコード値を表示する
- Explore another article to learn how `IronDrawing` is used for handling color and fonts
System.Drawing オブジェクトを AnyBitmap にキャストするにはどうすればよいですか?
System.Drawing からバーコードを読み取るには、オブジェクトを AnyBitmap にキャストする必要があります。 IronDrawing は使いやすさを考慮して設計されており、System.Drawing のイメージ オブジェクトを IronSoftware.Drawing と呼ばれる AnyBitmap のイメージ オブジェクトに暗黙的にキャストすることをサポートしています。
System.Drawing オブジェクト以外にも、他の型からのキャストをサポートしています。
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.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)
このコードは、System.Drawing オブジェクトと IronBarcode から IronDrawing までのシームレスな統合を示しています。 この互換性は、サポートされているバーコード形式ガイドに詳述されている、QRコード、Code 128、Code 39、その他多くのものを含む、さまざまなバーコード形式にわたって拡張されます。
なぜ暗黙的キャスティングは機能するのですか?
上記のコードでは、2 つのバーコード イメージを System.Drawing.Bitmap と System.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
よくある鋳造エラーとは
System.Drawing を AnyBitmap に変換する場合、開発者は次の問題に遭遇する可能性があります。
- Null参照例外: キャストする前に、
System.Drawingオブジェクトがnullでないことを確認してください。 2.サポートされていないフォーマットの例外: エキゾチックな画像フォーマットの中には、事前に変換が必要なものがあります。 3.メモリの問題:大きな画像は適切な処理パターンが必要
鋳造問題のトラブルシューティングについては、トラブルシューティング ガイドでバーコード認識時の一般的な問題の解決策を提供しています。
AnyBitmap オブジェクトからバーコードを読み取るにはどうすればいいですか?
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 パラメータを受け入れるいくつかのメソッドを提供します。 高度なシナリオについては、複数の 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)
複数の 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
他にどのようなIronDrawing機能が使用できますか?
IronSoftware.Drawing 機能はイメージのキャストを超えています。 バーコードやQRコードのスタイリングに役立つ色やフォントのような画像処理の側面を扱います。 IronDrawing を使用してQR コードをカスタマイズし、ロゴを追加する方法をご覧ください。
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)
特定の画像補正が必要なシナリオについては、画像補正ガイドで、バーコードの可読性を高めるフィルタの使用方法について詳しく説明しています。
System.Drawing ではなく、IronDrawing を選択する理由は何ですか?
IronDrawing は、System.Drawing に比べて次のような大きな利点があります。
1.クロスプラットフォーム サポート: System.Drawing ( .NET Core/5+ では Windows のみ) とは異なり、Windows、Linux、macOS でシームレスに動作します。
2.モダンアーキテクチャ: パフォーマンスとメモリ管理を向上させるために、SkiaSharp と ImageSharp に基づいて構築されています。
3.簡素化されたAPI : 使い慣れた@@--CODE-904のようなインターフェースを維持しながら、現代的な利便性を追加
4.アクティブな開発: メンテナンスモードのSystem.Drawingとは異なり、定期的な更新と改善が行われます
5.より優れた統合:ソフトウェア製品との最適なパフォーマンスのために特別に設計されています。
特にクラウド環境のデプロイメントに関する考慮事項については、 Azure へのデプロイメントと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にキャストし、画像に存在する全てのバーコードを読み取ります。

