How to Read Barcodes From System.Drawing in C
IronBarcode は、System.Drawing オブジェクトからバーコードを読み取り、AnyBitmap に変換することで、すべてのオペレーティングシステムで Microsoft の System.Drawing サポートの Windows のみという制約を解決します。
はじめに
System.Drawing オブジェクトは、.NET で画像処理タスクによく使用されます。 しかし、Microsoft は、MacOS や Linux の System.Drawing サポートを終了し、現在は Windows のみサポートしています。 この変更により、非WindowsオペレーティングシステムでIronBarcodeを使用する開発者に問題が生じました。なぜなら、バーコードの作業には通常グラフィックス、画像、フォントが含まれるからです。
この問題に対処するため、IronDrawingを導入しました。 Iron Softwareにより作成されたこの無料かつオープンソースのライブラリは、クロスプラットフォームサポートを簡素化し、シームレスな体験を提供します。 NuGet から IronBarcode をインストールすると、IronDrawing がプロジェクトに自動的に含まれます。
バーコード読み取りが初めての開発者には、基本的な概念と基本的な使用パターンを網羅した包括的な Reading Barcodes Tutorial を参照してください。 さまざまな画像フォーマットで作業している場合は、画像から BarCode を読み取るガイドに、追加のコンテキストと例が記載されています。
クイックスタート: 一行で AnyBitmap を使用してバーコードを読み取る
このスニペットは、System.Drawing.Bitmap を作成し、IronDrawing がそれを AnyBitmap に暗黙的にキャストすることで IronBarcode がバーコードをどのように読み取るかを示します。 たった1行で、どのOSの開発者でも迅速に結果を得ることができます。
最小限のワークフロー(5ステップ)
System.Drawingからバーコードを読み取るためのC#ライブラリをダウンロードします。IronDrawingを使用して、System.DrawingオブジェクトをAnyBitmapにキャストします。AnyBitmapオブジェクトからバーコードを読み取るには、Readメソッドを使用します。- コンソールに検出されたバーコード値を表示する
IronDrawingが色とフォントの処理にどのように使用されているかについては、別の記事を参照してください。
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 に変換する際、開発者は次のことに直面する可能性があります:
- ヌル参照例外:キャストする前に
System.Drawingオブジェクトがヌルでないことを確認してください 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 つの画像内の複数のバーコードの効率的な処理を実演しています:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-5.cs
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Set confidence threshold
ConfidenceThreshold = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
Imports System
' Advanced barcode reading with options
Dim readerOptions As New BarcodeReaderOptions With {
' Specify barcode types to search for
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
' Set confidence threshold
.ConfidenceThreshold = 0.95
}
' Read with specific options
Dim advancedResults = BarcodeReader.Read(anyBitmap, readerOptions)
複数の BarCode 結果を処理するにはどうすればよいですか?
上のコードは、前の例を拡張したものです。 AnyBitmap リストを作成した後、それを反復して各 AnyBitmap オブジェクトに対して Read メソッドを呼び出しました。結果として IronBarCode.BarcodeResults が返されました。 次に、バーコードの値をコンソールに出力するために、結果を反復しました。
複数の BarCode を処理する場合は、並列処理を活用してパフォーマンスを向上させます:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-6.cs
// 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 As New ConcurrentBag(Of BarcodeResult)()
Parallel.ForEach(barcodeFiles, Sub(file)
Dim bitmap As New Bitmap(file)
Dim anyBitmap As 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コードのスタイリングに役立つ色やフォントのような画像処理の側面を扱います。 QRコードにカスタムロゴを追加する方法を含めて、IronDrawing をどのように活用するかをご覧ください。
IronDrawing は、バーコード処理を補完する強力な画像操作機能を提供します:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-7.cs
// 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)
特定の画像補正が必要なシナリオについては、画像補正ガイドで、バーコードの可読性を高めるフィルタの使用方法について詳しく説明しています。
IronDrawing を System.Drawing より選ぶ理由は?
IronDrawing は System.Drawing に比べて説得力のある利点を提供します:
- クロスプラットフォームサポート:Windows、Linux、macOS でシームレスに動作 @
System.Drawing(Windows のみ .NET Core/5+) - モダンなアーキテクチャ:
SkiaSharpとImageSharpに基づいて構築されており、より良いパフォーマンスとメモリ管理を提供します - 簡略化された API:親しみやすい
System.Drawing-のようなインターフェースを保持しながら、最新の利便性を追加しています - アクティブな開発:定期的な更新と改善があり、
System.Drawingのメンテナンスモードとは異なります 5.より優れた統合:ソフトウェア製品との最適なパフォーマンスのために特別に設計されています。
デプロイに関する考慮事項、とりわけクラウド環境での考慮については、Azure へのデプロイおよび AWS へのデプロイ についてのガイドを参照してください。これには、IronDrawing を使用したクロスプラットフォーム互換性に関する具体的な注意事項が含まれています。
デスクトップアプリケーション、ウェブサービス、またはクラウドネイティブソリューションを構築する場合、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にキャストし、画像に存在する全てのバーコードを読み取ります。
IronBarcodeはバーコードの外観カスタマイズをサポートしていますか?
はい、IronBarcodeはカラー、サイズ、テキスト注釈を含むバーコードの外観に関する詳細なカスタマイズオプションを提供し、特定のデザイン要件に合わせて調整が可能です。
IronBarcodeはビジネスプロセスの効率向上にどのように役立ちますか?
IronBarcodeは迅速かつ正確なバーコード生成と読み取りを可能にし、手動データ入力エラーの減少、在庫および資産追跡の改善などにより、ビジネスプロセスの効率を向上させます。
プロジェクトにIronBarcodeを実装するために必要なプログラミングスキルは何ですか?
IronBarcodeをプロジェクトに実装するためには、C#プログラミングの基本的な知識があれば十分で、開発者をガイドするための簡単なメソッドと包括的なドキュメントが提供されています。
IronBarcodeは小規模プロジェクトと大規模エンタープライズアプリケーションの両方に適していますか?
IronBarcodeはスケーラブルかつ多用途に設計されており、小規模プロジェクトおよび強力なバーコードソリューションを必要とする大規模エンタープライズアプリケーションの両方に適しています。

