.NET MAUI でデスクトップ バーコード アプリケーション (スキャナ/ジェネレータ) を作成する方法
包括的なソフトウェア ソリューションでは、モバイル アクセシビリティだけが重要ではありません。 Windows や macOS などのデスクトップ オペレーティング システムにネイティブに展開する機能も同様に重要です。 このクロスプラットフォーム アプローチにより、企業は資産の追跡や管理などの大量のタスクにワークステーションの能力を最大限に活用できるようになります。
適切なデスクトップ サポートがないと、ワークフローが中断されたり、さらに悪いことに、機能の低いデバイスに強制的に実行されたりします。 これは、オフィススタッフがデスクを離れずに大量のコードを生成したり、スキャンを迅速に検証したりする必要がある在庫管理において特に重要です。
IronBarcode は、これらの機能を実装するために必要なツールを提供し、.NET MAUI アプリケーションがどのコンピューターでも確実に実行されることを保証します。
この記事では、IronBarcode を統合してデスクトップ バーコード スキャナーとデスクトップ バーコード ジェネレーターの両方を構築する方法について説明します。
IronBarcodeを使い始める
C# で .NET MAUI を使用してデスクトップ バーコード アプリケーションを作成する方法
- .NET MAUI (Desktop Platform Framework) と統合するための IronBarcode C# ライブラリをダウンロードします。
- ユーザーがアップロードしたバーコードを
Readます - バーコード値を.NET MAUI UIに表示する
CreateBarcodeを使用して文字列値を持つバーコードを生成する- 生成されたバーコードを.NET MAUI UIに表示し、画像として保存します。
.NET MAUI デスクトップ アプリケーション
ライブラリはそのままデスクトップ プラットフォームでネイティブに動作するため、IronBarcode を .NET MAUI アプリに統合するのは簡単です。 この例では、IronBarcode を使用してバーコード スキャナーとバーコード ジェネレーターの両方を個別に作成します。
まずはバーコードスキャナーから始めましょう。
バーコードスキャナインターフェースXAML
.NET MAUI インターフェイスでは、ユーザーが送信ボタンを使用してバーコード画像をアップロードできるようにするためのシンプルなインターフェイスが実装されています。 プロジェクト内のMainPage.xamlファイルを、以下に示す内容に置き換える必要があります。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ScrollView>
<VerticalStackLayout Spacing="25" Padding="30" VerticalOptions="Center">
<Label Text="Desktop Barcode Scanner"
FontSize="32"
HorizontalOptions="Center" />
<Image x:Name="ScannerImage"
HeightRequest="300"
WidthRequest="400"
BackgroundColor="#F0F0F0"
Aspect="AspectFit"
HorizontalOptions="Center" />
<Button Text="Select Image to Scan"
Clicked="OnScanButtonClicked"
HorizontalOptions="Center"
WidthRequest="200"/>
<Label x:Name="ResultLabel"
Text="Result will appear here..."
FontSize="20"
HorizontalOptions="Center"
HorizontalTextAlignment="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage><?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ScrollView>
<VerticalStackLayout Spacing="25" Padding="30" VerticalOptions="Center">
<Label Text="Desktop Barcode Scanner"
FontSize="32"
HorizontalOptions="Center" />
<Image x:Name="ScannerImage"
HeightRequest="300"
WidthRequest="400"
BackgroundColor="#F0F0F0"
Aspect="AspectFit"
HorizontalOptions="Center" />
<Button Text="Select Image to Scan"
Clicked="OnScanButtonClicked"
HorizontalOptions="Center"
WidthRequest="200"/>
<Label x:Name="ResultLabel"
Text="Result will appear here..."
FontSize="20"
HorizontalOptions="Center"
HorizontalTextAlignment="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>デスクトップバーコードスキャナ Logic CS
次に、ユーザーがボタンをクリックしたときのロジックを実装します。 OnScanButtonClickedイベント ハンドラーが UI のスキャン ボタンにアタッチされます。
まずPickPhotoAsyncを使用して、ユーザーがアップロードするバーコードを選択できるようにし、次にOpenReadAsyncを使用してファイル ストリームにアクセスします。 画像データは、 CopyToAsyncを使用してすぐにMemoryStreamにコピーされます。 これにより、データを画面に画像を表示するために同時に使用すると同時に、 Readメソッドでバーコードをスキャンできるようになります。
最後に、有効なバーコードが検出された場合はバーコード値が UI に表示されます。そうでない場合は、画像内にバーコードが見つからなかったことを示す赤いメッセージが表示されます。
using IronBarCode;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Your license key is set here
IronBarCode.License.LicenseKey = "YOUR_KEY"
}
private async void OnScanButtonClicked(object sender, EventArgs e)
{
try
{
var fileResult = await MediaPicker.Default.PickPhotoAsync();
if (fileResult != null)
{
// 1. Copy the file content into a byte array or MemoryStream immediately
// This ensures we have the data in memory before the file closes
byte[] imageBytes;
using (var stream = await fileResult.OpenReadAsync())
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
imageBytes = memoryStream.ToArray();
}
// 2. Set the Image Source for the UI
// We give the UI a FRESH stream from the bytes we just saved
ScannerImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));
// 3. Process the Barcode
// We give IronBarcode its OWN fresh stream from the same bytes
using (var processingStream = new MemoryStream(imageBytes))
{
// 4. Read the barcode with Read
var results = BarcodeReader.Read(processingStream);
// 5. Display barcode results
if (results != null && results.Count > 0)
{
// Successfully found barcode value
ResultLabel.Text = $"Success: {results[0].Value}";
ResultLabel.TextColor = Colors.Green;
}
else
{
// Image uploaded has no barcode or barcode value is not found
ResultLabel.Text = "No barcode detected.";
ResultLabel.TextColor = Colors.Red;
}
}
}
}
catch (Exception ex)
{
// Display any exception thrown in runtime
ResultLabel.Text = $"Error: {ex.Message}";
ResultLabel.TextColor = Colors.Red;
}
}
}
}using IronBarCode;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Your license key is set here
IronBarCode.License.LicenseKey = "YOUR_KEY"
}
private async void OnScanButtonClicked(object sender, EventArgs e)
{
try
{
var fileResult = await MediaPicker.Default.PickPhotoAsync();
if (fileResult != null)
{
// 1. Copy the file content into a byte array or MemoryStream immediately
// This ensures we have the data in memory before the file closes
byte[] imageBytes;
using (var stream = await fileResult.OpenReadAsync())
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
imageBytes = memoryStream.ToArray();
}
// 2. Set the Image Source for the UI
// We give the UI a FRESH stream from the bytes we just saved
ScannerImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));
// 3. Process the Barcode
// We give IronBarcode its OWN fresh stream from the same bytes
using (var processingStream = new MemoryStream(imageBytes))
{
// 4. Read the barcode with Read
var results = BarcodeReader.Read(processingStream);
// 5. Display barcode results
if (results != null && results.Count > 0)
{
// Successfully found barcode value
ResultLabel.Text = $"Success: {results[0].Value}";
ResultLabel.TextColor = Colors.Green;
}
else
{
// Image uploaded has no barcode or barcode value is not found
ResultLabel.Text = "No barcode detected.";
ResultLabel.TextColor = Colors.Red;
}
}
}
}
catch (Exception ex)
{
// Display any exception thrown in runtime
ResultLabel.Text = $"Error: {ex.Message}";
ResultLabel.TextColor = Colors.Red;
}
}
}
}バーコード値が見つかった出力

ご覧のとおり、アプリケーションはバーコードの結果とアップロードされたバーコード画像を表示します。
バーコード値が見つからない出力

ご覧のとおり、ユーザーがバーコードが含まれていない画像をアップロードすると、"バーコードが見つかりません"という赤いメッセージが表示されます。
デスクトップバーコードジェネレーター
次の部分では、同じ概念に基づいて、IronBarcode を MAUI に統合してバーコード ジェネレーターを作成します。
バーコードジェネレーターインターフェース XAML
ジェネレーターのインターフェースには、ドロップダウン メニューによるテキスト入力とバーコード タイプの選択を可能にするシンプルなフォームが実装されています。 生成と保存のプロセスをトリガーするボタンと、結果を表示するための画像ビューが含まれています。 MainPage.xaml ファイルを以下の内容に置き換える必要があります。
1D バーコードの完全なリストについてはここを、2D バーコードについてはここを参照してください。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ScrollView>
<VerticalStackLayout Spacing="20" Padding="30" VerticalOptions="Center">
<Label Text="Barcode Generator"
FontSize="32"
HorizontalOptions="Center" />
<Entry x:Name="BarcodeEntry"
Placeholder="Enter value (e.g. 12345 or https://ironsoftware.com)"
WidthRequest="300" />
<Picker x:Name="BarcodeTypePicker"
Title="Select Barcode Type"
WidthRequest="300">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>QRCode</x:String>
<x:String>Code128</x:String>
<x:String>EAN13</x:String>
<x:String>Code39</x:String>
<x:String>PDF417</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Button Text="Generate & Save"
Clicked="OnGenerateButtonClicked"
HorizontalOptions="Center"
WidthRequest="200" />
<Image x:Name="GeneratedImage"
HeightRequest="200"
WidthRequest="300"
BackgroundColor="#F0F0F0"
Aspect="AspectFit" />
<Label x:Name="StatusLabel"
FontSize="16"
HorizontalOptions="Center"
HorizontalTextAlignment="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage><?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ScrollView>
<VerticalStackLayout Spacing="20" Padding="30" VerticalOptions="Center">
<Label Text="Barcode Generator"
FontSize="32"
HorizontalOptions="Center" />
<Entry x:Name="BarcodeEntry"
Placeholder="Enter value (e.g. 12345 or https://ironsoftware.com)"
WidthRequest="300" />
<Picker x:Name="BarcodeTypePicker"
Title="Select Barcode Type"
WidthRequest="300">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>QRCode</x:String>
<x:String>Code128</x:String>
<x:String>EAN13</x:String>
<x:String>Code39</x:String>
<x:String>PDF417</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Button Text="Generate & Save"
Clicked="OnGenerateButtonClicked"
HorizontalOptions="Center"
WidthRequest="200" />
<Image x:Name="GeneratedImage"
HeightRequest="200"
WidthRequest="300"
BackgroundColor="#F0F0F0"
Aspect="AspectFit" />
<Label x:Name="StatusLabel"
FontSize="16"
HorizontalOptions="Center"
HorizontalTextAlignment="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>デスクトップバーコードジェネレーター Logic CS
次に、ボタンクリックイベントのロジックを実装します。 OnGenerateButtonClickedイベント ハンドラーが UI の生成ボタンに接続されます。
ユーザー入力は、テキストが存在し、タイプが選択されていることを確認するために検証され、その後、選択内容が正しいBarcodeEncodingにマップされます。 BarcodeWriter.CreateBarcodeは、画像を生成し、サイズを変更し、JPEG バイナリ データに変換するために使用されます。 その後、画像はMemoryStreamを使用して画面に表示されます。
最後に、生成されたバーコード ファイルは、 File.WriteAllBytesを使用してユーザーのデスクトップに直接保存され、保存場所を確認するためにステータス ラベルが更新されます。
using IronBarCode;
using System.IO; // Required for saving files
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
IronBarCode.License.LicenseKey = "YOUR-KEY";
// Set default selection
BarcodeTypePicker.SelectedIndex = 0;
}
private void OnGenerateButtonClicked(object sender, EventArgs e)
{
try
{
// 1. Get and Validate Input
string text = BarcodeEntry.Text;
if (string.IsNullOrWhiteSpace(text))
{
StatusLabel.Text = "Error: Please enter text.";
StatusLabel.TextColor = Colors.Red;
return;
}
if (BarcodeTypePicker.SelectedIndex == -1)
{
StatusLabel.Text = "Error: Please select a type.";
StatusLabel.TextColor = Colors.Red;
return;
}
// 2. Determine Encoding Type
string selectedType = BarcodeTypePicker.SelectedItem.ToString();
BarcodeEncoding encoding = BarcodeEncoding.QRCode;
switch (selectedType)
{
case "QRCode": encoding = BarcodeEncoding.QRCode; break;
case "Code128": encoding = BarcodeEncoding.Code128; break;
case "EAN13": encoding = BarcodeEncoding.EAN13; break;
case "Code39": encoding = BarcodeEncoding.Code39; break;
case "PDF417": encoding = BarcodeEncoding.PDF417; break;
}
// 3. Generate Barcode
var barcode = BarcodeWriter.CreateBarcode(text, encoding);
barcode.ResizeTo(400, 200); // Optional resizing
// 4. Convert to Bytes (JPEG)
var bytes = barcode.ToJpegBinaryData();
// 5. Update UI
GeneratedImage.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
// 6. Save to Desktop automatically
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string fileName = $"barcode_{DateTime.Now:yyyyMMdd_HHmmss}.jpg";
string fullPath = Path.Combine(desktopPath, fileName);
File.WriteAllBytes(fullPath, bytes);
// 7. Show Success Message
StatusLabel.Text = $"Saved to Desktop:\n{fileName}";
StatusLabel.TextColor = Colors.Green;
}
catch (Exception ex)
{
StatusLabel.Text = $"Error: {ex.Message}";
StatusLabel.TextColor = Colors.Red;
}
}
}
}using IronBarCode;
using System.IO; // Required for saving files
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
IronBarCode.License.LicenseKey = "YOUR-KEY";
// Set default selection
BarcodeTypePicker.SelectedIndex = 0;
}
private void OnGenerateButtonClicked(object sender, EventArgs e)
{
try
{
// 1. Get and Validate Input
string text = BarcodeEntry.Text;
if (string.IsNullOrWhiteSpace(text))
{
StatusLabel.Text = "Error: Please enter text.";
StatusLabel.TextColor = Colors.Red;
return;
}
if (BarcodeTypePicker.SelectedIndex == -1)
{
StatusLabel.Text = "Error: Please select a type.";
StatusLabel.TextColor = Colors.Red;
return;
}
// 2. Determine Encoding Type
string selectedType = BarcodeTypePicker.SelectedItem.ToString();
BarcodeEncoding encoding = BarcodeEncoding.QRCode;
switch (selectedType)
{
case "QRCode": encoding = BarcodeEncoding.QRCode; break;
case "Code128": encoding = BarcodeEncoding.Code128; break;
case "EAN13": encoding = BarcodeEncoding.EAN13; break;
case "Code39": encoding = BarcodeEncoding.Code39; break;
case "PDF417": encoding = BarcodeEncoding.PDF417; break;
}
// 3. Generate Barcode
var barcode = BarcodeWriter.CreateBarcode(text, encoding);
barcode.ResizeTo(400, 200); // Optional resizing
// 4. Convert to Bytes (JPEG)
var bytes = barcode.ToJpegBinaryData();
// 5. Update UI
GeneratedImage.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
// 6. Save to Desktop automatically
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string fileName = $"barcode_{DateTime.Now:yyyyMMdd_HHmmss}.jpg";
string fullPath = Path.Combine(desktopPath, fileName);
File.WriteAllBytes(fullPath, bytes);
// 7. Show Success Message
StatusLabel.Text = $"Saved to Desktop:\n{fileName}";
StatusLabel.TextColor = Colors.Green;
}
catch (Exception ex)
{
StatusLabel.Text = $"Error: {ex.Message}";
StatusLabel.TextColor = Colors.Red;
}
}
}
}生成されたバーコードで出力

ご覧のとおり、アプリケーションは生成されたバーコードを表示し、ユーザーのデスクトップに保存します。
出力失敗

特定のバーコード タイプには、入力値に対する制限と形式要件があります。 バーコードの生成に失敗した場合、アプリケーションは上記のように IronBarcode 例外を表示します。 各バーコード タイプのフォーマットについては、それぞれ1Dおよび2D の詳細を参照してください。
上記の例 (デスクトップ バーコード スキャナーとデスクトップ バーコード ジェネレーター) をテストするには、このサンプル プロジェクトをダウンロードしてください。
よくある質問
.NET MAUI とは何ですか?
.NET MAUIは、C#とXAMLを使用してネイティブモバイルアプリとデスクトップアプリを作成するためのクロスプラットフォームフレームワークです。開発者は、単一のコードベースを使用してAndroid、iOS、macOS、Windows向けのアプリケーションを構築できます。
IronBarcode を .NET MAUI アプリケーションでどのように使用できますか?
IronBarcode は .NET MAUI アプリケーションに統合することで、バーコードの生成とスキャン機能を有効にできます。複数のデスクトップ プラットフォームでバーコードを簡単に作成・読み取ることができます。
IronBarcode はどのような種類のバーコードを生成できますか?
IronBarcode は、QR コード、コード 128、コード 39、UPC、EAN など、さまざまなバーコード形式の生成をサポートしており、あらゆるデスクトップ アプリケーションのニーズに幅広く対応できます。
IronBarcode で構築されたデスクトップ アプリケーションを使用してバーコードをスキャンすることは可能ですか?
はい、IronBarcode を使用するとデスクトップ アプリケーションでバーコードをスキャンできるため、ユーザーはアプリケーションのインターフェイスを通じてバーコード情報を迅速かつ効率的にデコードできます。
バーコード アプリケーションに IronBarcode を使用する利点は何ですか?
IronBarcodeは、高い精度、高速性、そして使いやすさを提供します。.NET MAUIとシームレスに統合され、デスクトップアプリケーションにおけるバーコード生成とスキャンを包括的にサポートします。
IronBarcode は大量のバーコードデータを処理できますか?
はい、IronBarcode は大量のバーコード データを効率的に処理するように設計されているため、広範なバーコード タスクの処理を必要とするアプリケーションに適しています。
バーコードのスキャンと生成には別のライブラリが必要ですか?
いいえ、IronBarcode は単一のライブラリ内でバーコードのスキャンと生成の両方の機能を提供するため、デスクトップ アプリケーションの開発プロセスが簡素化されます。
IronBarcode を .NET MAUI で使用するためのシステム要件は何ですか?
IronBarcode を使用するには、.NET MAUI と互換性のある .NET 環境が必要です。Windows、macOS、および .NET MAUI アプリケーションを実行できるその他のプラットフォームをサポートしています。
IronBarcode はどのようにしてバーコードの精度を保証するのでしょうか?
IronBarcode は、高度なアルゴリズムを使用してバーコードの生成とスキャンの両方で高精度を確保し、バーコード データのエンコードまたはデコード時にエラーが発生する可能性を低減します。
IronBarcode は商用プロジェクトと個人プロジェクトの両方で使用できますか?
はい、IronBarcode は商用プロジェクトと個人プロジェクトの両方に使用でき、さまざまなプロジェクト要件に合わせて柔軟なライセンス オプションを提供します。






