C#のSystem.DrawingからBarCodeを読み取る方法
IronBarcodeは、IronDrawingを通じてAnyBitmapに自動的に変換することで、すべてのオペレーティングシステム上のSystem.Drawingオブジェクトからバーコードを読み取ることを可能にし、System.Drawingサポートに関するマイクロソフトの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の開発者でも迅速に結果を得ることができます。
今すぐ NuGet で PDF を作成してみましょう:
NuGet パッケージ マネージャーを使用して IronBarcode をインストールします
このコード スニペットをコピーして実行します。
var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));実際の環境でテストするためにデプロイする
最小限のワークフロー(5ステップ)
System.Drawingからバーコードを読み取るためのC#ライブラリをダウンロードします。- Utilize `IronDrawing` to cast `System.Drawing` objects into `AnyBitmap`
- Use the
Readmethod 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からAnyBitmapと呼ばれるIronSoftware.Drawing画像オブジェクトへの暗黙のキャストをサポートしています。
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.csusing 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);このコードは、IronDrawingを通して、System.DrawingオブジェクトとIronBarcode間のシームレスな統合を示しています。 この互換性は、サポートされているバーコード形式ガイドに詳述されている、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
}よくある鋳造エラーとは
System.DrawingをAnyBitmapに変換するとき、開発者は以下のことに遭遇するかもしれません:
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.csusing 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);
}
}基本的な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);複数の 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}");
}他にどのような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);特定の画像補正が必要なシナリオについては、画像補正ガイドで、バーコードの可読性を高めるフィルタの使用方法について詳しく説明しています。
System.DrawingよりもIronDrawingを選ぶ理由?
IronDrawingは、System.Drawingと比較して魅力的な利点を提供します:
1.クロスプラットフォームのサポート:System.Drawing(.NET Core/5+ではWindowsのみ)とは異なり、Windows、Linux、macOSでシームレスに動作します。 2.最新のアーキテクチャ:SkiaSharpとImageSharpをベースに構築されており、パフォーマンスとメモリ管理が向上しています。 3.簡略化されたAPI:使い慣れたSystem.Drawingのようなインターフェイスを維持しながら、現代的な利便性を追加します。 4.積極的な開発:メンテナンスモードのSystem.Drawingとは異なり、定期的な更新と改善 5.より優れた統合:IronSoftware製品との最適なパフォーマンスのために特別に設計されています。
特にクラウド環境へのデプロイについては、AzureへのデプロイとAWSへのデプロイのガイドを参照してください。AWSへのデプロイには、IronDrawingを使用したクロスプラットフォームの互換性に関する具体的な注意事項が含まれています。
デスクトップ アプリケーション、Web サービス、クラウド ネイティブ ソリューションのいずれを構築する場合でも、IronDrawingは、バーコード処理コードがすべてのプラットフォームで移植性と効率性を維持することを保証し、最新の .NET 開発に理想的な選択肢となります。
よくある質問
Windows 以外のプラットフォームで System.Drawing オブジェクトから BarCode を読み取る方法を教えてください。
IronBarcodeはIronDrawingを通してSystem.Drawingオブジェクトからのクロスプラットフォームのバーコード読み取りを自動的に処理し、AnyBitmapフォーマットに変換します。これはSystem.DrawingがWindows専用であるというマイクロソフト社の制限を解決し、MacOSやLinuxシステムでシームレスにバーコードを読み取ることを可能にします。
IronDrawingとは何ですか?なぜバーコード読み取りと一緒に含まれているのですか?
IronDrawingはIronSoftwareによって作成された無償のオープンソースライブラリで、グラフィックス操作をクロスプラットフォームでサポートします。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にキャストし、画像に存在する全てのバーコードを読み取ります。






