C#/VB.NET: Generate or Scan QR Code

What is a QR Code

A QR code is a machine-readable code consisting of an array of black and white squares, typically used for storing URL or other information for reading by the camera on a smartphone. The QR code system was invented in 1994, now it’s popular worldwide due to its faster readability and greater storage capacity compared to standard UPC barcodes.

In this article, I will demonstrate how to generate or scan QR code using C# and VB.NET.

C# Library to Generate or Scan QR Code

In order to generate or scan QR code, I will use Spire.Barcode for .NET API which supports generating and reading up to 39 kinds of 1D and 2D barcodes.

You can either download the API from here or install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then adding the following code:

PM> Install-Package Spire.Barcode

Generate QR Code in C# and VB.NET

The following steps show you how to generate a QR code using C# and VB.NET:

  • Initialize an instance of the BarcodeSettings class.
  • Specify the barcode type as QR code by setting the BarcodeSettings.Type property as BarCodeType.QRCode.
  • Set other properties such as error collection level, barcode module width, and data for the QR code using BarcodeSettings.QRCodeECL, BarcodeSettings.X and BarcodeSetting.Data properties.
  • Initialize an instance of the BarCodeGenerator class.
  • Generate QR code image using BarCodeGenerator.GenerateImage() method.
  • Save the image to file using System.Drawing.Image.Save() method.

C#

using Spire.Barcode;
using System.Drawing;

namespace GenerateQRCode
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the BarcodeSettings class
            BarcodeSettings barcodeSettings = new BarcodeSettings
            {
                //Specify the barcode type as QRCode
                Type = BarCodeType.QRCode,
                //Set error collection level
                QRCodeECL = QRCodeECL.M,
                //Set width of the barcode module
                X = 2.5f,
                //Set barcode data 
                Data = "https://medium.com/",
                //Set text to display on top of the barcode (By default, it's the same as the barcode data)
                //Data2D = "Medium",
                //Hide text from barcode
                ShowText = false,
            };

            //Initialize an instance of the BarCodeGenerator class
            BarCodeGenerator barCodeGenerator = new BarCodeGenerator(barcodeSettings);
            //Generate QRCode image
            Image image = barCodeGenerator.GenerateImage();
            //Save the image to file
            image.Save("QRCode.png");
        }
    }
}

VB.NET

Imports Spire.Barcode

Namespace GenerateQRCode
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of the BarcodeSettings class
            Dim barcodeSettings As BarcodeSettings = New BarcodeSettings With {
    'Specify the barcode type as QRCode
    .Type = BarCodeType.QRCode,
    'Set error collection level
    .QRCodeECL = QRCodeECL.M,
    'Set width of the barcode module
    .X = 2.5F,
    'Set barcode data 
    .Data = "https://medium.com/",
    'Set text to display on top of the barcode (By default, it's the same as the barcode data)
    'Data2D = "Medium",
    'Hide text from barcode
    .ShowText = False
}

            'Initialize an instance of the BarCodeGenerator class
            Dim barCodeGenerator As BarCodeGenerator = New BarCodeGenerator(barcodeSettings)
            'Generate QRCode image
            Dim image As Image = barCodeGenerator.GenerateImage()
            'Save the image to file
            image.Save("QRCode.png")
        End Sub
    End Class
End Namespace
Generate QR code in C#

Scan QR Code in C# and VB.NET

The following steps show you how to scan a QR code image using C# and VB.NET:

  • Scan the QR code image using BarcodeScanner.ScanOne(string filePath) method and save the result into a string object.
  • Print out the result using Console.WriteLine() method.

C#

using Spire.Barcode;

namespace ScanQRCode
{
    internal class Program
    {
        static void Main(string[] args)
        {          
            //Scan the QR code image
            string barcodeData = BarcodeScanner.ScanOne("QRCode.png");
            //Print out the result
            Console.WriteLine(barcodeData);
            Console.ReadKey();
        }
    }
}

VB.NET

Imports Spire.Barcode

Namespace ScanQRCode
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Scan the QR code image
            Dim barcodeData As String = BarcodeScanner.ScanOne("QRCode.png")
            'Print out the result
            Console.WriteLine(barcodeData)
            Console.ReadKey()
        End Sub
    End Class
End Namespace
Scan or Read QR Code in C#

In case you have an image that contains multiple barcodes, and you want to read the data of all the QR codes in the image, you can use the following code:

C#

using Spire.Barcode;

namespace ScanQRCodes
{
    internal class Program
    {
        static void Main(string[] args)
        {        
            //Scan the QR code image and save the data into a string array
            string[] results = BarcodeScanner.Scan("Barcodes.png", BarCodeType.QRCode);

            //Iterate through the array
            foreach (string result in results)
            {
                //Print out the result
                Console.WriteLine(result);
            }

            Console.ReadKey();
        }
    }
}

VB.NET

Imports Spire.Barcode

Namespace ScanQRCodes
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Scan the QR code image and save the data into a string array
            Dim results As String() = BarcodeScanner.Scan("Barcodes.png", BarCodeType.QRCode)

            'Iterate through the array
            For Each result In results
                'Print out the result
                Console.WriteLine(result)
            Next

            Console.ReadKey()
        End Sub
    End Class
End Namespace

Get a Free Trial License

The library will generate an evaluation message on the generated QR code image, you can request a free 30 days trial license to remove the messsage from the image.

C#/VB.NET: Generate QR Code with a Custom Logo Image

When you generate a QR code, you might want to add a custom image such as your company’s logo or your personal profile image to it.  In this article, I will describe how to achieve this task programmatically in C# and VB.NET.

Add Reference

In order to generate QR code and add logo image, I will use Spire.Barcode for .NET API which supports generating and reading up to 39 kinds of 1D and 2D barcodes.

You can either download the API from here or install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then adding the following code:

PM> Install-Package Spire.Barcode

Generate QR Code with a Custom Logo Image

The following are the steps to generate a QR code with a custom logo image:

1. Instantiate a BarcodeSettings object.
2. Specify barcode type, error correction level, barcode width and data etc. using BarcodeSettings.Type, BarcodeSettings.QRCodeECL, BarcodeSettings.X and BarcodeSetting.Data attributes etc.
3. Add a custom image to the QR code using BarcodeSettings.QRCodeLogoImage attribute.
4. Instantiate a BarCodeGenerator object.
5. Generate QR code image using BarCodeGenerator.GenerateImage() method.
6. Save the image using Image.Save() method.

C#

using Spire.Barcode;
using System.Drawing;

namespace GenerateQRCodeWithLogo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a license file if you have one
            //Spire.License.LicenseProvider.SetLicenseFileFullPath("licensePath");

            //Instantiate a BarcodeSettings object
            BarcodeSettings settings = new BarcodeSettings();

            //Specify barcode type, data, etc.
            settings.Type = BarCodeType.QRCode;
            settings.QRCodeECL = QRCodeECL.M;
            settings.ShowText = false;
            settings.X = 2.5f;
            string data = "https://twitter.com";
            settings.Data = data;
            settings.Data2D = data;

            //Add an image to QR code
            settings.QRCodeLogoImage = Image.FromFile(@"C:\Users\Administrator\Desktop\Logo.png");

            //Instantiate a BarCodeGenerator object
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            //Generate QR image
            Image image = generator.GenerateImage();
            //Save the image
            image.Save("QR.png", System.Drawing.Imaging.ImageFormat.Png);            
        }
    }
}

VB.NET

Imports Spire.Barcode

Namespace GenerateQRCodeWithLogo
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Load a license file if you have one
            'Spire.License.LicenseProvider.SetLicenseFileFullPath("licensePath")

            'Instantiate a BarcodeSettings object
            Dim settings As BarcodeSettings = New BarcodeSettings()

            'Specify barcode type, data, etc.
            settings.Type = BarCodeType.QRCode
            settings.QRCodeECL = QRCodeECL.M
            settings.ShowText = False
            settings.X = 2.5F
            Dim data As String = "https://twitter.com"
            settings.Data = data
            settings.Data2D = data

            'Add an image to QR code
            settings.QRCodeLogoImage = Image.FromFile("C:\Users\Administrator\Desktop\Logo.png")

            'Instantiate a BarCodeGenerator object
            Dim generator As BarCodeGenerator = New BarCodeGenerator(settings)
            'Generate QR image
            Dim image As Image = generator.GenerateImage()
            'Save the image
            image.Save("QR.png", Drawing.Imaging.ImageFormat.Png)
        End Sub
    End Class
End Namespace

The generated QR code with logo image:

QR code with Logo Image

Get a Free Trial License

The library will generate an evaluation watermark on the barcode image, you can request a free 30 days trial license to remove the watermark from the image.

Design a site like this with WordPress.com
Get started