How to Generate QR Code in C#

Barcodes, specially two dimensional matrix barcodes like QR codes, are common place nowadays. So the C# developers often question how to generate QR code in C# applications. This can be done quickly with few lines of code using Aspose.BarCode for .NET.

You can easily create barcodes or QR code in your .NET applications, including Windows applications, or Web applications using C# with the steps given in this answer.

Steps to Generate QR Code in C#

  1. Nuget package of Aspose.BarCode for .NET is required first
  2. Next, use Aspose.Barcode and Aspose.BarCode.Generation namespaces
  3. Add Aspose license using SetLicense method
  4. Now, create an instance of BarCodeGenerator Class and set QR as encode type
  5. Set code text which you want to add to the QR code
  6. Set required properties of the DocumentBuilder object
  7. As a last step, save QR code as image format

In this example, we’re saving the QR code image as PNG, however, you can save your QR code in Bmp, Gif, Jpeg, PNG, Tiff, TiffInCmyk, EMF, or SVG image formats. You just need to pass the image format while saving the generated QR code.

Example for Generating QR Code in C#

using System;
//Add reference to Aspose.BarCode API
//Use following two namespaces to run QR generator code
using Aspose.BarCode;
using Aspose.BarCode.Generation;
namespace GenerateQRCode
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before generating QR code using Aspose.Barcode
Aspose.BarCode.License AsposeBarCodeLicense = new Aspose.BarCode.License();
AsposeBarCodeLicense.SetLicense(@"c:\asposelicense\license.lic");
//Create an instance of BarCode generator class
//specify Barcode Encode Type as QR
BarcodeGenerator QRCodeGenerator = new BarcodeGenerator(EncodeTypes.QR);
//set text to be encoded as generated QR code
QRCodeGenerator.CodeText = "Text To Encode";
//Save the generated QR code based barcode in image format
//Aspose.Barcode support multiple image formats when saving generated QR code
QRCodeGenerator.Save("Generated_QR_Code.png", BarCodeImageFormat.Png);
}
}
}

If you’re developing your own QR code generator or adding this QR generation feature in your application, the steps and sample given above can save you a lot of time and effort.

 English