How to Read & Scan Barcode in C# Using NET MAUI

This tutorial demonstrates how to create a barcode scanning app using .NET MAUI and Iron Barcode. The process begins with setting up a .NET MAUI cross-platform application in Visual Studio, ensuring the necessary development workload is installed. Next, the Iron Barcode library is integrated via the NuGet package manager. The user interface is created using XAML, featuring a scrollable view, a vertical stack layout, and interactive elements such as buttons and an image tag. The main functionality is implemented in C#, where methods handle image selection, barcode scanning, and text copying to the clipboard. Users can select an image containing a barcode, scan it with Iron Barcode, and display the decoded text efficiently. The app supports various barcode types, showcasing its versatility. This guide provides a comprehensive walkthrough of building a barcode scanner app, highlighting the integration of Iron Barcode with .NET MAUI. Explore more possibilities with Iron Barcode and enhance your app development experience.

<!-- XAML for User Interface -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BarcodeScannerApp.MainPage">
    <ScrollView>
        <VerticalStackLayout Padding="10">
            <Button Text="Select Image" Clicked="OnSelectImageClicked"/>
            <Image x:Name="SelectedImage" HeightRequest="200"/>
            <Button Text="Scan Barcode" Clicked="OnScanBarcodeClicked"/>
            <Label x:Name="DecodedTextLabel" Text="Decoded text will appear here."/>
            <Button Text="Copy to Clipboard" Clicked="OnCopyToClipboardClicked"/>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>
<!-- XAML for User Interface -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BarcodeScannerApp.MainPage">
    <ScrollView>
        <VerticalStackLayout Padding="10">
            <Button Text="Select Image" Clicked="OnSelectImageClicked"/>
            <Image x:Name="SelectedImage" HeightRequest="200"/>
            <Button Text="Scan Barcode" Clicked="OnScanBarcodeClicked"/>
            <Label x:Name="DecodedTextLabel" Text="Decoded text will appear here."/>
            <Button Text="Copy to Clipboard" Clicked="OnCopyToClipboardClicked"/>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>
XML
// C# Code-behind for Main Functionality
using IronBarCode;
using Microsoft.Maui.Controls;

namespace BarcodeScannerApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        // Handler for selecting an image from device storage.
        private async void OnSelectImageClicked(object sender, EventArgs e)
        {
            // Code to select an image
            var result = await FilePicker.PickAsync(new PickOptions
            {
                PickerTitle = "Select a Barcode Image"
            });

            if (result != null)
            {
                // Displaying selected image in the Image control
                SelectedImage.Source = ImageSource.FromFile(result.FullPath);
            }
        }

        // Handler for scanning barcode from the selected image.
        private void OnScanBarcodeClicked(object sender, EventArgs e)
        {
            try
            {
                var bitmap = IronBarCode.Bitmap.LoadFromFile(result.FullPath);

                // Using Iron Barcode to read the barcode from the image
                BarcodeResult result = BarcodeReader.ReadASingleBarcode(bitmap);

                // Displaying the decoded text in the label
                DecodedTextLabel.Text = result.Text;
            }
            catch (Exception ex)
            {
                DisplayAlert("Error", "Failed to read barcode: " + ex.Message, "OK");
            }
        }

        // Handler for copying decoded text to clipboard.
        private async void OnCopyToClipboardClicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DecodedTextLabel.Text))
            {
                await Clipboard.Default.SetTextAsync(DecodedTextLabel.Text);
                await DisplayAlert("Success", "Text copied to clipboard!", "OK");
            }
            else
            {
                await DisplayAlert("Error", "No decoded text to copy.", "OK");
            }
        }
    }
}
// C# Code-behind for Main Functionality
using IronBarCode;
using Microsoft.Maui.Controls;

namespace BarcodeScannerApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        // Handler for selecting an image from device storage.
        private async void OnSelectImageClicked(object sender, EventArgs e)
        {
            // Code to select an image
            var result = await FilePicker.PickAsync(new PickOptions
            {
                PickerTitle = "Select a Barcode Image"
            });

            if (result != null)
            {
                // Displaying selected image in the Image control
                SelectedImage.Source = ImageSource.FromFile(result.FullPath);
            }
        }

        // Handler for scanning barcode from the selected image.
        private void OnScanBarcodeClicked(object sender, EventArgs e)
        {
            try
            {
                var bitmap = IronBarCode.Bitmap.LoadFromFile(result.FullPath);

                // Using Iron Barcode to read the barcode from the image
                BarcodeResult result = BarcodeReader.ReadASingleBarcode(bitmap);

                // Displaying the decoded text in the label
                DecodedTextLabel.Text = result.Text;
            }
            catch (Exception ex)
            {
                DisplayAlert("Error", "Failed to read barcode: " + ex.Message, "OK");
            }
        }

        // Handler for copying decoded text to clipboard.
        private async void OnCopyToClipboardClicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DecodedTextLabel.Text))
            {
                await Clipboard.Default.SetTextAsync(DecodedTextLabel.Text);
                await DisplayAlert("Success", "Text copied to clipboard!", "OK");
            }
            else
            {
                await DisplayAlert("Error", "No decoded text to copy.", "OK");
            }
        }
    }
}
' C# Code-behind for Main Functionality
Imports IronBarCode
Imports Microsoft.Maui.Controls

Namespace BarcodeScannerApp
	Partial Public Class MainPage
		Inherits ContentPage

		Public Sub New()
			InitializeComponent()
		End Sub

		' Handler for selecting an image from device storage.
		Private Async Sub OnSelectImageClicked(ByVal sender As Object, ByVal e As EventArgs)
			' Code to select an image
			Dim result = Await FilePicker.PickAsync(New PickOptions With {.PickerTitle = "Select a Barcode Image"})

			If result IsNot Nothing Then
				' Displaying selected image in the Image control
				SelectedImage.Source = ImageSource.FromFile(result.FullPath)
			End If
		End Sub

		' Handler for scanning barcode from the selected image.
		Private Sub OnScanBarcodeClicked(ByVal sender As Object, ByVal e As EventArgs)
			Try
				Dim bitmap = IronBarCode.Bitmap.LoadFromFile(result.FullPath)

				' Using Iron Barcode to read the barcode from the image
				Dim result As BarcodeResult = BarcodeReader.ReadASingleBarcode(bitmap)

				' Displaying the decoded text in the label
				DecodedTextLabel.Text = result.Text
			Catch ex As Exception
				DisplayAlert("Error", "Failed to read barcode: " & ex.Message, "OK")
			End Try
		End Sub

		' Handler for copying decoded text to clipboard.
		Private Async Sub OnCopyToClipboardClicked(ByVal sender As Object, ByVal e As EventArgs)
			If Not String.IsNullOrEmpty(DecodedTextLabel.Text) Then
				Await Clipboard.Default.SetTextAsync(DecodedTextLabel.Text)
				Await DisplayAlert("Success", "Text copied to clipboard!", "OK")
			Else
				Await DisplayAlert("Error", "No decoded text to copy.", "OK")
			End If
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Further Reading: .NET MAUI Barcode Scanner

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Read Barcodes From Images in C#
NEXT >
How to create Barcodes as Streams in C# Using IronBarcode