Home > .NET Barcode Reader > .NET Barcode Reading Guide > .NET QR-Code Barcode Reader for C#, VB.NET Applications

How to Read and Scan QR Code in C# ASP.NET and MVC Applications

Scan & Read QR-Code barcodes in C#.NET, VB.NET, ASP.NET projects
  • Easily read & scan QR-Code barcodes in .NET applications
  • Quickly scan & decode QR Code 2d barcodes from specified area inside the image document
  • Decoding & reading QR Code barcode images in JPEG, GIF, PNG, BMP, TIFF file formats
  • 100% build in C#.NET, complying with .NET 2.0 and later versions
  • Complete sample C#.net, VB.net sample codes for Visual C# & VB.NET developers
  • Mature & reliable QR Code .NET Barcode Reader Component since 2003
In this tutorial, developers will learn how to scan and read QR Code barcode data from images in C# and VB.NET ASP.NET web applications, as well as in Windows Forms and WPF desktop applications.

The QR Code reader functionality is part of the C# Barcode Reader component, which enables scanning and decoding of QR Code barcodes from image files. This component is designed for easy integration into C# and VB.NET projects, supporting a wide range of .NET platforms including ASP.NET Core, ASP.NET Framework, WinForms, and WPF. The library provides comprehensive scanning capabilities, including support for multiple image formats, region specific scanning, and detailed barcode property extraction.



Download & Install

You can download and install the nuget package BarcodeLib.BarcodeReader in the Visual Studio IDE.

You can also download the free trial package from our site here Download Free Trial Package. And add downloaded BarcodeLib.BarcodeReader.dll to your .NET project reference.




How to scan, read QR Code from images using C#?

Scan and read QR-Code barcode in C# is an easy and simple task. The following C# source will scan and read QR Code data from image file in C# application.

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);



Scan colored QR Code in ASP.NET web application.



Scan QR Code from memory objects

Using BarcodeLib C# Barcode Reader library, you can also scan and read QR Codes from image memory objects (such as Steam, byte array) in C# program.

byte[] dataBytes = File.ReadAllBytes("qrcode-sample.png");
Stream barcodeInStream = new MemoryStream(dataBytes);
OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode(barcodeInStream, options);


Scanning and Reading All QR Code Information from an Image File Using C#

The ScanInDetails() method returns detailed information about each scanned QR Code in BarcodeDetail objects.

Key properties of BarcodeDetail include:
  • TypeID: Check the scanned it is a QR Code, it will return the int value 9
  • GetTypeName(): If it is a QR Code, it will return string QRCODE
  • Corners: Four coordinate points (PointF) defining the detected QR Code region.
  • IsGS1Compitable: Indicates whether the QR Code is GS1 compatible.
  • GetDataBytes(): Retrieves the QR Code data as a byte array.
  • GetMessage(): Retrieves the QR Code data as a formatted string.
  • getImageFileIndex(): If scanning QR Code from multiple frame images, such as TIFF, GIF, it will return the scanned QR Code located image frame index. The first image frame is 0.
  • getImageFilePath(): Return the scanned image file path. Return "", if the scanned image is in memory.


Extracting QR Code Barcode Properties

After obtaining a BarcodeDetail object, call getBarcodeExtraInfos() to retrieve QR Code specific properties:
  • ECL: Error correction level.
  • Encoding: Encoding data mode; QRCodeEncoding.Auto indicates multiple modes combined.
  • Version: QR Code version used during encoding.
  • hasStructuredAppend: Whether the symbol is in Structured Append mode.
  • Count: Total number of symbols in the Structured Append group.
  • Index: Index position of the symbol within the Structured Append group (starting from 1).
  • Parity: 8 bit parity data.

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);
foreach (BarCode data in datas)
{
    QRCodeInfo barcodeInfo = (QRCodeInfo)data.getBarcodeExtraInfos();
    QRCodeErrorCorrectionLevel ecl = barcodeInfo.getECL();
    QRCodeEncoding encoding = barcodeInfo.getEncoding();
    QRCodeVersion version = barcodeInfo.getVersion();
    bool isStructuredAppend = barcodeInfo.hasStructuredAppend();
    if (isStructuredAppend)
    {
        int count = barcodeInfo.getStructuredAppendCount();
        int index = barcodeInfo.getStructuredAppendIndex();
        int parity = barcodeInfo.getStructuredAppendParity();
    }

}


Processing Scanned QR Code Data Messages Using C#

QR Code supports multiple character sets and industry standard data formats. The library provides methods to handle these data types:
  • ASCII text (printable and non printable characters)
  • Unicode text
  • Binary data
  • GS1 data elements


ASCII Text Characters

The following code scans a QR Code and replaces the non printable carriage return character (\r) with the visible label [CR].

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);
foreach (BarCode barcode in datas)
{
    String qrcodeData = barcode.GetMessageInASCII();
}



Unicode Text

To decode Unicode text encoded with UTF 8, retrieve the byte array and decode it.

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);
foreach (BarCode data in datas)
{
    string dataMessage = data.GetMessage();
    string dataMessageFromBytes = System.Text.Encoding.UTF8.GetString(data.GetDataBytes());
}



GS1 Data Message

The library simplifies scanning GS1 QR Codes and parsing GS1 data messages. The IsGS1Compitable property indicates GS1 compliance, and GetMessage() returns a formatted string with parentheses delimited AI codes.

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);
foreach (BarCode data in datas)
{
    //  Verify whether it is a GS1 QR Code
    if (data.IsGS1Compitable)
    {
        //  Get message in string format.
        //  Eg. "(415)5412345678908(3911)710125"
        String msg = data.GetMessage();
        Debug.WriteLine("Raw Data: '{0}'", data.Data);
        Debug.WriteLine("Text Message: {0}", msg);
        //  Retrieve each AI and its data from the message.
        //  Eg.
        //  AI:   415
        //  Data: 5412345678908
        //  AI:   3911
        //  Data: 710125
        String[] vals = msg.Split(new char[] { '(', ')' }, 
            StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < vals.Length; i += 2)
        {
            Debug.WriteLine("AI:    {0}", vals[i]);
            Debug.WriteLine("Data:  {0}", vals[i + 1]);
        }
    }
}



Advanced QR Code Image Scanning Features Using C#

The library provides several methods to improve scanning speed and flexibility.


Scan a Single QR Code

When the image contains only one QR Code, set setMaxOneBarcodePerPage() in OptimizeSetting to improve performance by stopping after the first detection.

Set setMaxOneBarcodePerPage() to true, if there is maximum one barcode per image or per page in tiff or pdf document.
  • If setMaxOneBarcodePerPage() is true, the C# barcode reader library will stop scanning the barcode immediately, once detects one barcode.
  • If setMaxOneBarcodePerPage() is false (default value), the library will use total 5 algorithms and each will scan the whole image from 4 directions.
OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
options.setMaxOneBarcodePerPage(true);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);


Scan Multiple QR Codes and Other Barcode Formats

Multiple barcode types can be scanned simultaneously.

OptimizeSetting options = new OptimizeSetting(
    new int[] { BarcodeReader.QRCODE, BarcodeReader.CODE128 });
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);




Scan Image Regions to Read QR Codes

Scan the partial image instead of the whole file. If the barcode is always located one specified area in the image, you can set and let the library scan that area only. And it will reduce lots of scanning time, CPU and memory usage.

You just specify the left top point and right bottom point of the area (the point X, Y values are expressed in percentage of the whole image, so image most left top point is (0%, 0%), and most right bottom point is (100%, 100%)).

You can specify multiple areas to scan the QR Code inside one image file.

OptimizeSetting options = new OptimizeSetting(BarcodeReader.QRCODE);
List<ScanArea> areas = new List<ScanArea>();
areas.Add(new ScanArea(new PointF(0, 0), new PointF(50, 60)));
options.setAreas(areas);
BarCode[] datas = BarcodeReader.ReadBarcode("qrcode-sample.png", options);


Summary

This tutorial has provided a comprehensive guide to integrating QR Code reading capabilities into .NET applications using C# and VB.NET. Developers can leverage the BarcodeLib Barcode Reader SDK to scan and decode QR Code symbols from image files, extract detailed barcode properties, and process various data formats including ASCII, Unicode, binary, and GS1 structured messages. The component seamlessly integrates into ASP.NET Core, ASP.NET Framework, WinForms, and WPF applications, enabling robust QR Code reading functionality across multiple .NET environments.