How to Build a QR Code Scanner in Blazor
Blazor Server is a .NET web framework that runs C# on the server and pushes UI updates to the browser over a SignalR connection. IronQR integrates directly into Blazor Server, enabling server-side QR code scanning from browser-uploaded images without any JavaScript.
In this how-to guide, we'll build a Blazor Server page that accepts an image upload and decodes any embedded QR code using IronQR.
How to Scan a QR Code in Blazor
- Install the IronQR C# library to scan QR codes on the web
- Add an
InputFilecomponent to accept image uploads from the browser - Stream the uploaded file to a temporary path on the server
- Load the image and wrap it in a
QrImageInput - Call
Readand display the decoded value in the Blazor component
Prerequisites
- Visual Studio 2022 with the ASP.NET and web development workload installed
- A Blazor Server project targeting .NET 8 or later
Install IronQR
Install the IronQR library using the NuGet Package Manager Console in Visual Studio. Navigate to Tools > NuGet Package Manager > Package Manager Console and run:
Install-Package IronQR
Alternatively, search for IronQR on NuGet and install the latest version.
Blazor Component Layout
The scanner UI uses Blazor's built-in InputFile component for browser-native file selection, a button to trigger the scan, and a paragraph to display the decoded result. No JavaScript interop is required.
Add a new Razor component Scanner.razor with the following markup:
@page "/scanner"
@using IronQr
@using IronSoftware.Drawing
<h3>QR Code Scanner</h3>
<InputFile OnChange="OnFileSelected" accept="image/*" />
<br /><br />
<button @onclick="ScanQRCode" disabled="@(qrImageSrc == null)">Scan QR Code</button>
<p>Result: @scannedText</p>
Sample Input
Use the QR code below as a test image. Save it to your device, upload it via the file picker in the app, then click Scan QR Code. The decoded value should display as https://ironsoftware.com.
Sample QR code — encodes https://ironsoftware.com
QR Code Scanning with IronQR
When a file is selected, OnFileSelected streams the browser upload to a temporary file on the server. When the scan button is clicked, ScanQRCode loads the file into an AnyBitmap, passes it to QrReader.Read(), and writes the first decoded value to scannedText, which Blazor automatically renders in the component.
Add the following @code block to Scanner.razor:
:path=/static-assets/qr/content-code-examples/how-to/blazor-qr-code-scanner.cs
using IronQr;
using IronSoftware.Drawing;
private string? qrImageSrc;
private string scannedText = string.Empty;
private async Task OnFileSelected(InputFileChangeEventArgs e)
{
var file = e.File;
var tempPath = Path.Combine(Path.GetTempPath(), file.Name);
await using var stream = file.OpenReadStream(maxAllowedSize: 10_000_000);
await using var fs = File.Create(tempPath);
await stream.CopyToAsync(fs);
qrImageSrc = tempPath;
}
private async Task ScanQRCode()
{
// Load the scanned QR code
var inputBmp = AnyBitmap.FromFile(qrImageSrc!);
var imageInput = new QrImageInput(inputBmp);
var reader = new QrReader();
// Read the scanned QR code
var results = reader.Read(imageInput);
// Check if a result was found
var firstResult = results.FirstOrDefault();
if (firstResult != null)
{
// Save the QR code value as a string
scannedText = firstResult.Value;
}
else
{
scannedText = "No QR code found in the selected image.";
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.IO
Imports Microsoft.AspNetCore.Components.Forms
Imports System.Threading.Tasks
Private qrImageSrc As String = Nothing
Private scannedText As String = String.Empty
Private Async Function OnFileSelected(e As InputFileChangeEventArgs) As Task
Dim file = e.File
Dim tempPath = Path.Combine(Path.GetTempPath(), file.Name)
Await Using stream = file.OpenReadStream(maxAllowedSize:=10000000)
Await Using fs = File.Create(tempPath)
Await stream.CopyToAsync(fs)
End Using
End Using
qrImageSrc = tempPath
End Function
Private Async Function ScanQRCode() As Task
' Load the scanned QR code
Dim inputBmp = AnyBitmap.FromFile(qrImageSrc)
Dim imageInput = New QrImageInput(inputBmp)
Dim reader = New QrReader()
' Read the scanned QR code
Dim results = reader.Read(imageInput)
' Check if a result was found
Dim firstResult = results.FirstOrDefault()
If firstResult IsNot Nothing Then
' Save the QR code value as a string
scannedText = firstResult.Value
Else
scannedText = "No QR code found in the selected image."
End If
End Function
InputFile.OpenReadStream streams the uploaded bytes directly to a server-side temp file. AnyBitmap.FromFile decodes the image format, and QrReader.Read returns an IEnumerable<QrResult> with one entry per QR code found. FirstOrDefault safely handles images with no QR code without throwing an exception.
Output
After selecting a QR code image and clicking Scan QR Code, the decoded value renders in the result paragraph below the controls.
Decoded QR code value rendered in the Blazor component
Download the Project
Click here to download the complete BlazorQrScanner project.
Conclusion
IronQR integrates into a Blazor Server application with no JavaScript. InputFile provides browser-native file selection, and QrReader.Read handles decoding entirely on the server. The same pattern scales to scanning multiple QR codes per image by iterating over the full results collection instead of calling FirstOrDefault.
For more on reading QR codes and the available scan modes, see the Read QR Codes from Image and Read QR Codes with Scan Modes guides.
Frequently Asked Questions
What is the primary use of IronQR in a Blazor Server application?
IronQR is primarily used in a Blazor Server application to enable server-side QR code scanning from images uploaded via the browser, without the need for JavaScript.
How do you install the IronQR library in a Blazor project?
To install the IronQR library in a Blazor project, use the NuGet Package Manager Console in Visual Studio and run the installation command, or search for 'IronQR' on NuGet and install the latest version.
What component is used in Blazor to handle image uploads for QR code scanning?
The `InputFile` component is used in Blazor to handle image uploads, allowing users to select images from the browser for QR code scanning.
How does IronQR decode QR codes in a Blazor application?
In a Blazor application, IronQR decodes QR codes by loading the selected image into an `AnyBitmap` and then using `QrReader.Read()` to extract and display the QR code's data.
Can IronQR handle images with multiple QR codes?
Yes, IronQR can handle images with multiple QR codes by iterating over the `IEnumerable
Do you need JavaScript to implement QR code scanning with IronQR in Blazor?
No, you do not need JavaScript to implement QR code scanning with IronQR in Blazor, as the entire process is handled server-side.
What is the role of `AnyBitmap` in the IronQR scanning process?
`AnyBitmap` is used to decode the image format of the uploaded file, preparing it for QR code reading with IronQR.
How does Blazor display the result of a QR code scan?
Blazor displays the result of a QR code scan by rendering the decoded value in a paragraph element within the component's UI.
What happens if an image with no QR code is uploaded in the Blazor scanner?
If an image with no QR code is uploaded, calling `FirstOrDefault` on the `IEnumerable
Is it possible to download the complete Blazor QR Scanner project?
Yes, you can download the complete Blazor QR Scanner project from the provided link on the guide's webpage.

