.NET QR Code Generator (Code Example Tutorial)
This tutorial will use IronBarcode as a core library to generate QR codes that have many applications in different industries.
Introducing IronBarcode
Additional Features
- IronBarcode can read and write most barcode types and QR standards, such as UPC A/E, EAN 8/13, Code 39/93/128, ITF, MSI, RSS 14/Expanded, Databar, and CodaB.
- IronBarcode preprocesses barcode pictures automatically to improve reading efficiency and precision.
- IronBarcode can read scans and live video frames, correcting rotation, noise, distortion, and skewing.
- IronBarcode can be utilized across multiple cores and threads (very useful for server applications that perform batch processing).
- IronBarcode can find one or more barcodes automatically in single- and multi-page documents.
- IronBarcode supports 32- and 64-bit architectures and can be used in both .NET implementations (.NET Core and .NET Framework).
- IronBarcode supports Console, Desktop, Cloud, and Web Applications on PC and mobile platforms.
- IronBarcode can generate QR code images for a range of files and stream formats, including PDF, JPG, TIFF, GIF, BMP, PNG, and HTML.
Generating QR Codes Using IronBarcode
The proceeding sections of this article present an example to demonstrate how to generate QR codes easily.
Step 1. Create a New Project
Open Visual Studio, and select New Project from the File Menu.
Choose the Console App template in the window that appears, and click Next.
Creating a New Console App in Visual Studio for QR Code Generation
In the Project name text field, type in any project name to your liking (for example, QR Code Generator), and specify a location for the new project in the Location field. Afterward, click the Next button to continue.
Choosing a name and location for a new Console Application in Visual Studio to create QR codes
Choose a .NET Framework from the Framework dropdown menu (here, we are using .NET 6.0 (Long term support)) and click Create.
Creating the new Console App in Visual Studio under .NET 6.0 Framework
Step 2. Install the Barcode Library
2.1 Using IronBarcode
You can download and install the IronBarcode library in four ways.
These ways are:
- Using Visual Studio's NuGet Package Manager UI,
- Using Visual Studio's Package Manager Console,
- Downloading it directly from the NuGet website, or
- Downloading it directly from the IronBarcode website.
2.1.1 Using Visual Studio's Package Manager UI
From the menu bar, go to Tools > NuGet Package Manager > Manage NuGet Packages for solution... to open the Package Manager UI.
Installing the IronBarcode library using Visual Studio's NuGet Package Manager UI
Alternatively, you can right-click the name of your project from the Solution Explorer Window, and select Manage NuGet Packages... from the context menu.
Click on the Browse tab, and type in Barcode in the search field. Select IronBarcode from the list of related packages (shown as the first result in the image below), choose your project in the pane on the right and click the Install button.
Searching for the IronBarcode library in the Package Manager UI. It will most likely appear before all other libraries in the search results
2.1.2 Using Visual Studio's Package Manager Console
Go to Tools > NuGet Package Manager > Package Manager Console. Enter the following command in the command-line panel that appears and press ENTER:
Install-Package BarCode
The above command will download and install the library into the current project.
2.1.3 Download The Library from the NuGet Website
Search for the Barcode library page on the NuGet Gallery website in your browser, (or click on this NuGet BarCode
package link to access the page directly).
Click on the Download package link from the menu on the right-hand side to save the library on your computer. Next, double-click the downloaded library from your File Manager to install it into your project automatically. Finally, reload your project, and it will be ready to go.
2.1.4 Download the Library from the IronBarcode Website
Click on IronBarcode's homepage to download the latest .NET barcode DLL. Once downloaded, follow the steps below to add the package to your project:
- Right-click the project from the Solution Explorer Panel, and click on Add > Reference.
Adding the IronBarcode DLL into the project directly from Visual Studio
- Click on the Browse button and navigate to the location where you have extracted the DLL. With the DLL selected, click OK to add it to your project.
Inserting the IronBarcode DLL as a new reference in your project
Step 3. Generate a QR Code Image
3.1 Using IronBarcode in a Windows/Console Application
Generate a new QR code by invoking the CreateQrCode
method from the QRCodeWriter
class:
using IronBarCode; // Add IronBarCode namespace at the top
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
qrCode.SaveAsPng("MyQR.png");
using IronBarCode; // Add IronBarCode namespace at the top
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
qrCode.SaveAsPng("MyQR.png");
Imports IronBarCode ' Add IronBarCode namespace at the top
Private qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0)
qrCode.SaveAsPng("MyQR.png")
The CreateQrCode
method accepts one required parameter, which is the data to be encoded in the code image (can be a String or a Stream). The method also accepts three optional parameters:
- The width and height of the graphic (500px by 500px by default)
- An error correction level. IronBarcode offers four levels of error correction: Low, Medium, High, and Highest. By default,
CreateQrCode
uses the highest correction levelQRCodeWriter.QrErrorCorrectionLevel.Highest
- A QR symbol version number. See this page for a list of valid versions. A value of 0 (the default value) instructs the method to use the correct version number based on the data that it will encode.
The example above generates a 500-pixel by 500-pixel graphic using the medium level of error correction. The subsequent call to the SaveAsPng
method on the generated QR code saves it as a PNG file at a given file location.
The result of calling QrCodeWriter.CreateQrCode using the aforementioned parameters
Next, the sample code below uses the CreateQrCodeWithLogo
method to add a company logo to a generated QR code, a typical use-case scenario for any business.
var qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png", 500);
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed);
qrWithLogo.SaveAsPng("Logo_QR_Code.png");
var qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png", 500);
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed);
qrWithLogo.SaveAsPng("Logo_QR_Code.png");
Dim qrWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World", "qrlogo.png", 500)
qrWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed)
qrWithLogo.SaveAsPng("Logo_QR_Code.png")
In the example above, a String value of "Hello, World" is encoded into a new QR code that embeds an image located at a specified file path. The image is automatically sized to fit the QR code square grid, being aligned to the size that will allow QR Code readers to still read the pure code data.
The next line of code above uses the ChangeBarCodeColor
method to change the color of the QR code to dark red. Here, we do the coloring using one of the available System color class types provided by C# (namely, System.Drawing.Color.DarkRed). It is possible to specify colors in the HTML hex color notation, as the line of code below shows:
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
qrWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"))
The last line of code in the code example above calls the SaveAsPng method to save the QR code as a PNG file. QR codes can be saved in other file formats such as HTML:
qrWithLogo.SaveAsHtmlFile("test.html");
qrWithLogo.SaveAsHtmlFile("test.html");
qrWithLogo.SaveAsHtmlFile("test.html")
Refer to this documentation page for a full list of file types to which QR codes can be saved.
Generate QR Codes in different colors, in different file formats, and using different images the methods available in IronBarcode's QRCodeWriter class
3.2 Using IronBarcode in a Web Application
The line of code demonstrates how IronBarcode can be used within a .NET MVC Web Application:
public IActionResult Index()
{
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
qrCode.SaveAsPng("Demo.png");
return File("Demo.png", "image/png", "Demo.png", true);
}
public IActionResult Index()
{
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
qrCode.SaveAsPng("Demo.png");
return File("Demo.png", "image/png", "Demo.png", true);
}
Public Function Index() As IActionResult
Dim qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0)
qrCode.SaveAsPng("Demo.png")
Return File("Demo.png", "image/png", "Demo.png", True)
End Function
The example above operates very similarly to the previous examples. First, a QR code is generated, and then it's returned to the client in the response body.
Conclusion
IronBarcode is one of the fastest and the most powerful libraries available for reading and writing barcodes. This simple library is supported by various operating systems, it supports many barcode formats and it is easy to use.
The licensing and legal information for using IronBarcode can be found on the licensing page. IronBarcode is free to use under the Free Developer License. The premium (paid) developer licensing includes one year of free support and product updates.
Frequently Asked Questions
What is this library used for?
IronBarcode is a C# library that provides functions for reading and writing barcodes, including QR codes, with customizable options and support for various barcode standards.
What are the main features of this library?
IronBarcode can read and write most barcode types, preprocess barcode images for better reading efficiency, support multi-core processing, and generate QR codes for a variety of file formats including PDF, JPG, and PNG.
How do you install this library?
IronBarcode can be installed via Visual Studio's NuGet Package Manager UI, Package Manager Console, or downloaded directly from the NuGet website or IronBarcode website.
How can I generate a QR code using this library?
To generate a QR code, use the QRCodeWriter.CreateQrCode method in a C# application, specifying the data, size, and error correction level.
Can this library be used in web applications?
Yes, IronBarcode can be used in .NET MVC Web Applications to generate and return QR codes as image files.
What file formats can QR codes be saved in using this library?
QR codes generated with IronBarcode can be saved in several formats, including PNG, HTML, and others as supported by the library.
Does this library support customization of QR code appearance?
Yes, IronBarcode allows customization of QR code colors and can embed images, such as company logos, within QR codes.
What are the licensing terms for this library?
IronBarcode is free under the Free Developer License, with premium options available that include support and product updates.
What error correction levels does this library support for QR codes?
IronBarcode supports four levels of error correction for QR codes: Low, Medium, High, and Highest.
Is this library compatible with different .NET implementations?
Yes, IronBarcode supports both .NET Core and .NET Framework, as well as 32- and 64-bit architectures.