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
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2023/02/c-generate-qr-code.png?w=255)
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
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2023/02/c-scan-qr-code.png?w=602)
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.