C# QR Code Generator Library
How to generate QR Code barcode using C# in ASP.NET Core web application
Create QR Code Barcode on C# Images, TIFF, PDF, Word, Excel and PowerPoint
In this C# tutorial, you will learn how to create a QR Code 2d barcode image using C# in ASP.NET Core Razor pages
- Setup a new ASP.NET Core web app and install packages and QR Code generator dlls
- Create a Razor page to input QR Code data message
- Generate and display QR Code image on the Razor page
How to create QR Code barcodes in ASP.NET Core web app using C#
Prerequisites
Download and install the following software on your computer
How to create QR Code barcode in ASP.NET Core web app using C#?
1. Create a new ASP.NET Core web app
Start Visual Studio 2022 and choose "Create a new project".
Choose "ASP.NET Core Web App" in the dialog, and then press "Next" button.
Create a new ASP.NET Core Web App project with name "RasterEdge.QRCodeGenerator.ASPNETCoreDemo".
Choose Framework as ".NET 6.0 (Long-term support)", and then press "Create" button.
Now, all Visual Studio generated files could be found in the Solution Explorer panel.
Open the file "Program.cs" and ensure that it has enabled following Middlewares.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
2. Install NuGet Packages and RasterEdge QR Code Generator DLLs
To install QR Code generator dlls in your .NET Core projects, please go to page How to install DLLs on .NET Core app
3. Add C# code to create QR Code in ASP.NET Core web app
Copy all following codes to replace the content of the file "Index.cshtml".
The cshtml page code will
- Add one text box to allow user to key in QR Code data message
- One submit button to generate and update QR Code image
- An image to display the generated QR Code barcode
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<form asp-page="./Index" method="post">
<br />
QR Code Data Message:
<input type="text" id="tb1" name="tbMessage" style="width: 600px; " asp-for="@Model.Message" />
<br />
<br />
<input type="submit" value="Create Barcode" />
</form>
<br />
<img src="data:image/png;base64,@Model.BarcodeImage" />
</div>
Copy all following codes to replace the content in the file "Index.cshtml.cs".
The cshtml.cs C# source code will
- Create a new QRCode barcode object
- Set the QR Code object with data message from text box
- Apply the QR Code data mode, barcode dimension size (width and height) settings, and image resolution
- Call method QRCode.DrawBarcode() to create QR Code in a PNG image file and return the image file in byte array
- Display the generated QR Code on the web page
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RasterEdge.XImage.BarcodeCreator;
namespace RasterEdge.QRCodeGenerator.ASPNETCoreDemo.Pages
{
public class IndexModel : PageModel
{
public String Message { get; set; } = "";
public String BarcodeImage { get; set; } = "";
public void OnGet()
{
}
public Task<IActionResult> OnPost()
{
this.Message = Request.Form["tbMessage"].ToString();
QRCode barcode = new QRCode();
barcode.Data = this.Message;
barcode.DataMode = QRCodeDataMode.Auto;
barcode.AutoResize = true;
barcode.UOM = UnitOfMeasure.INCH;
barcode.BarcodeWidth = 2F;
barcode.BarcodeHeight = 2F;
barcode.Resolution = 300;
byte[] dataBytes = barcode.DrawBarcode(OutputFileType.PNG);
BarcodeImage = System.Convert.ToBase64String(dataBytes);
return Task.FromResult<IActionResult>(Page());
}
}
}
4. Run web app
It is done. Now run the project.
It will launche the Kestrel web server and opens the default web browser.
Key in the QR Code characters in text box, and click the button "Create Barcode" to generate and display QR Code barcode image on the web page.
How to create, print QR Code with options in C# ASP.NET, Windows project?
QR Code error correction mode using C#
Four levels of Reed-Solomon error correction (referred to as L, M, Q and H in increasing order of
capacity) allowing recovery of the QR Code barcode data.
- L: 7% (default)
- M: 15%
- Q: 25%
- H: 30%
Using QR Code generator C# library, you shall set QR Code error correction mode through property "ECL". The default value is
QRCodeECL.L. Here is the sample C# source code.
QRCode barcode = new QRCode();
barcode.Data = "QR Code";
barcode.DataMode = QRCodeDataMode.Auto;
/*
QRCode Error Correction Level. Default is QRCodeECL.L (0).
QRCodeECL.L (0)
QRCodeECL.M (1)
QRCodeECL.Q (2)
QRCodeECL.H (3)
*
*/
barcode.ECL = QRCodeECL.M;
QR Code Version in C#
There are forty sizes of QR Code barcode referred to as Version 1, Version 2 ... Version 40.
View details at
How to set QR Code Versions
Using C# QR Code generator, you shall set property Version with 40 options, from
QRCodeVersion.V1 to QRCodeVersion.V40.
// Selecte format mode
// from V1 to V40
barcode.Version = QRCodeVersion.V3;
QR Code Structured Append mode using C#
A large QR Code data message can be divided and stored in up to 16 QR Code barcodes in a QR Code structured format mode.
In QR Code generator C# library, to implement Structured Append feature, you should apply the following property options :
- StructuredAppend: Set value to true, to enable Structured Append mode in QR Code
- SymbolCount: Total number of symbols for one QR Code data message
- SymbolIndex: The position of current symbol in the sequence (The first symbol index is 0)
barcode.StructuredAppend = true;
barcode.SymbolCount = 3; // The data message will be stored in three QR Code barcodes
barcode.SymbolIndex = 0; // This symbol is the first one
QR Code FNC1 in first position
This QR Code FNC1 mode indicator identifies the current QR Code encoding data formatted according to the
GS1 Application Identifiers standard.
To enable this mode using C# QR Code generator library, you need apply the following property in the C# source code.
- Set property
FNC1 with value FNC1.FNC1_1ST_POS
QRCode barcode = new QRCode();
barcode.FNC1 = FNC1.FNC1_1ST_POS;
QR Code Extended Channel Interpretation (ECI) mode in C#
The Extended Channel Interpretation (ECI) protocol defined in the AIM Inc. ECI allows the output data stream to have interpretations different
from that of the default character set.
The default interpretation for QR Code is
ECI 000003 representing the
ISO/IEC 8859-1 character set.
In QR Code generator C# library, to change encoding character set using ECI, you need apply the following property:
- ECI: Six digits ECI value for the new character set defined by the AIM. For example: 000013 for ISO/IEC 8859-11
- ProcessTilde: Set the ProcessTilde property to true. You need convert new character data to byte array.
Complete C# sample code to encode Thai text in QR Code barcode, using ECI.
QRCode barcode = new QRCode();
// Set to Byte mode
barcode.DataMode = QRCodeDataMode.Byte;
// Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes.
barcode.ECI = 13;
// Set flag to enable '~' in data message.
barcode.ProcessTilde = true;
// ISO/IEC 8859-11 Latin/Thai alphabet
// Byte values from 0xA0 to 0xFF are used for Thai characters.
// Eg. ก - 0xA1 - 161
barcode.Data = "~161";
barcode.drawBarcode(@"C:\QRCode_ECI000013.png");
Common Asked Questions
What is the full form of "QR Code"?
It stands for "Quick Response" which refers to its ability to be scanned quickly, it's a type of two-dimensional barcode that stores data in a grid of black and white squares.
Using XImage.Barcode Generator C# library, you are able to generate standard QR Code in ASP.NET Core, MVC, Blazor web application and WinForms, WPF, Console applications.
Are there any options better than a QR Code?
Some 2D barcode formats have features similar to QR Codes. One notable example is Data Matrix, and it's popular in industrial environments such as manufacturing and logistics.
Using XImage.Barcode Generator C# library, you can create QR Code, Micro QR Code and other 2d barcodes in Visual Studio .NET projects.
What's the minimum printable size for a QR Code?
The QR Code ISO standard doesn't set a minimum size. But most phone-based QR scanners require a minimum of 2 x 2 cm.
Using XImage.Barcode Generator C# library, you can generate any standard QR Code with any size in C# class.
Can QR Codes function if they aren't black and white?
Yes. While black and white is standard, provided the contrast remains high enough for reliable scanning.
Using XImage.Barcode Generator C# library, you will print styled QR Code with customized bar and space colors in C# web and desktop applications.
How much data can a single QR Code hold?
A full-size QR Code with low error correction can store up to 7,089 digits, 4,296 alphanumeric characters, 2,953 bytes of binary data, or 1,817 Kanji characters.
C# Barcode Generator library fully supports ISO standard.