Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article shows how developers can use the IronBarcode library to make QR codes in C# projects.
IronBarcode is a C# Barcode & QR code Library that allows you to read and generate barcodes and QR codes easily in any .NET C# applications. It prioritizes speed, accuracy, and ease of use for C#
IronBarcode is a user-friendly library that allows developers to easily add, read, and style QR codes in C# projects, usually within minutes. It enables developers to create QR codes in formats like Aztec, Data Matrix, MaxiCode. Different barcode types are available using IronBarcode, e.g., Code 39, Code 128, RSS14, etc.
IronBarcode is compatible with all .NET Framework languages, i.e., C#, F#, and VB.NET. It supports all .NET platforms listed below:
To create QR codes in Blazor web apps in C#, we require the following components:
Blazor Server App: Creating Blazor Server app is the primary task. Follow the given steps below to create a Blazor server web application.
Open your IDE or Visual Studio. Click Create a new project.
Choose Blazor Server App from the available project options.
Next, set the location and name your project.
Next, set the additional information for your project. Select an appropriate .NET Framework Version. IronBarcode supports .NET versions 2 through 7, with .NET 7 being the latest.
Once everything is installed and ready, add the following assembly reference to your Blazor barcode application's "Imports.razor" file:
@using IronBarCode
@using IronBarCode
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@using IronBarCode
IronBarcode must be licensed to be used for deployment. You can get your free trial license key from here. Add the following lines of code to Program.cs file:
using IronBarCode;
License.LicenseKey = "YOUR-KEY-HERE";
using IronBarCode;
License.LicenseKey = "YOUR-KEY-HERE";
Imports IronBarCode
License.LicenseKey = "YOUR-KEY-HERE"
The following steps will help generate QR codes in the best possible way in Blazor Server application:
First, we are going to add some HTML code to the "Index.razor" page. The code consists of some headings, an input field, and buttons.
Use the code below to add the components to Blazor barcode app:
Welcome to Blazor QR Code Generator
Enter the value to Generate QR Code:
Generate QR Code
Generated QR Code:
Welcome to Blazor QR Code Generator
Enter the value to Generate QR Code:
Generate QR Code
Generated QR Code:
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Welcome @to Blazor QR Code Generator Enter the value @to Generate QR Code: Generate QR Code Generated QR Code:
The output is as follows:
The text field is used to get input from the user, and the generate button will call the onclick
method to create a QR code. The created QR code will be displayed under the "Generated QR Code:" heading. Now, let's create the QRCodeGenerater
method.
QrCodeGenerator
MethodIt's time to use the powerful IronBarcode library in Visual Studio to generate QR codes. The @code
section is added to "Index.razor" page. Let's first see the complete code below:
@code {
private string qrCode = "";
private string text = "";
private void QRCodeGenerator()
{
GeneratedBarcode myQRCode = QRCodeWriter.CreateQrCode(text);
qrCode = Convert.ToString(myQRCode.ToHtmlTag());
}
}
@code {
private string qrCode = "";
private string text = "";
private void QRCodeGenerator()
{
GeneratedBarcode myQRCode = QRCodeWriter.CreateQrCode(text);
qrCode = Convert.ToString(myQRCode.ToHtmlTag());
}
}
code
If True Then
private String qrCode = ""
private String text = ""
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' private void QRCodeGenerator()
' {
' GeneratedBarcode myQRCode = QRCodeWriter.CreateQrCode(text);
' qrCode = Convert.ToString(myQRCode.ToHtmlTag());
' }
End If
In the above code, there are two private string variables. qrCode
will display the generated QR
code, and text will get the user input from the input field. Then, in the QRCodeGenerator
method, myQRCode
is created using the QRCodeWriter
class's CreateQrCode
method. The CreateQrCode
method provides several parameters to enhance the code output. You can customize the size of the QR code, set error correction code words to maintain the error correction level and change the QrVersion
. The code below helps you work with other parameters as well:
QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0)
QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0)
Finally, the generated Blazor barcode is converted to an HTML tag and then to a qrCode
string.
Get more detail on IronBarcode features from this code examples page.
Add the code below in HTML components in "Index.razor" page, after H4 heading to display qrCode
:
@((MarkupString)qrCode)
@((MarkupString)qrCode)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@((MarkupString)qrCode)
You can also save it to a PNG file using the SaveAsPng
method:
myQRCode.SaveAsPng("myQRCode.png");
myQRCode.SaveAsPng("myQRCode.png");
myQRCode.SaveAsPng("myQRCode.png")
You can also create a QR code on page load and display it for the users to scan for data. The following code helps you achieve this task asynchronously:
@((MarkupString)qrCode)
@code {
private string qrCode = "";
protected override async Task OnInitializedAsync()
{
await Task.Run(() => qrCode = Convert.ToString(QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0).ToHtmlTag()));
}
}
@((MarkupString)qrCode)
@code {
private string qrCode = "";
protected override async Task OnInitializedAsync()
{
await Task.Run(() => qrCode = Convert.ToString(QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0).ToHtmlTag()));
}
}
'INSTANT VB WARNING: The following constructor is declared outside of its associated class:
'ORIGINAL LINE: @((MarkupString)qrCode) @code
Private Sub New(ByVal qrCode As (MarkupString))
private String qrCode = ""
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' protected override async Task OnInitializedAsync()
' {
' await Task.Run(() => qrCode = Convert.ToString(QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.High, 0).ToHtmlTag()));
' }
End Sub
In this article, we learned how to generate QR codes in Blazor Server application using IronBarcode in C#. IronBarcode is a flexible library and provides all necessary methods and support for Blazor apps.
IronBarcode easily converts plain text, images, videos, or any other data to QR codes. You can control the code size, error correction levels, and its version. You can use IronBarcode to scan barcodes in Blazor applications. A complete example is here.
IronBarcode is free for development but must be licensed for commercial and deployment purposes. You can try the free trial to test all its functionality.
9 .NET API products for your office documents