Skip to footer content
USING IRONBARCODE

.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.

.NET QR Code Generator (Code Example Tutorial), Figure 1: Creating a New Console App in Visual Studio for QR Code Generation 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.

.NET QR Code Generator (Code Example Tutorial), Figure 2: Choosing a name and location for a new Console Application in Visual Studio to create QR codes. 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.

.NET QR Code Generator (Code Example Tutorial), Figure 3: Creating the new Console App in Visual Studio under .NET 6.0 Framework 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.

.NET QR Code Generator (Code Example Tutorial), Figure 4: Installing the IronBarcode library using Visual Studio's NuGet 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.

.NET QR Code Generator (Code Example Tutorial), Figure 5: Searching for the IronBarcode library in the Package Manager UI. It will most likely appear before all other libraries in the search results. 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:

  1. Right-click the project from the Solution Explorer Panel, and click on Add > Reference.

.NET QR Code Generator (Code Example Tutorial), Figure 6: Adding the IronBarcode DLL into the project directly from Visual Studio. Adding the IronBarcode DLL into the project directly from Visual Studio

  1. 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.

.NET QR Code Generator (Code Example Tutorial), Figure 7: Inserting the IronBarcode DLL as a new COM reference in 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")
$vbLabelText   $csharpLabel

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:

  1. The width and height of the graphic (500px by 500px by default)
  2. An error correction level. IronBarcode offers four levels of error correction: Low, Medium, High, and Highest. By default, CreateQrCode uses the highest correction level QRCodeWriter.QrErrorCorrectionLevel.Highest
  3. 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.

.NET QR Code Generator (Code Example Tutorial), Figure 8: The result of calling QrCodeWriter.CreateQrCode using the aforementioned parameters. 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")
$vbLabelText   $csharpLabel

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"))
$vbLabelText   $csharpLabel

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")
$vbLabelText   $csharpLabel

Refer to this documentation page for a full list of file types to which QR codes can be saved.

.NET QR Code Generator (Code Example Tutorial), Figure 9: Generate QR Codes in different colors, in different file formats, and using different images the methods available in IronBarcode's QRCodeWriter class. 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
$vbLabelText   $csharpLabel

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

How do I generate a QR code in a .NET application?

You can generate a QR code in a .NET application by using the QRCodeWriter.CreateQrCode method in IronBarcode. This method allows you to specify the QR code data, size, and error correction level.

What are the customization options available for QR codes?

IronBarcode allows customization of QR codes, including changing colors and embedding images such as company logos. These features enhance the visual appeal and brand integration of the QR codes.

How can I install the IronBarcode library in my project?

You can install IronBarcode in your project through Visual Studio's NuGet Package Manager UI, Package Manager Console, or by downloading it from the NuGet website.

Can IronBarcode be used to read barcodes from video frames?

Yes, IronBarcode can process video frames, allowing it to read barcodes in real-time by correcting for rotation and noise, thereby enhancing the barcode reading efficiency.

What file formats can be used to save QR codes generated with IronBarcode?

QR codes generated with IronBarcode can be saved in multiple formats, including PNG and HTML, providing flexibility for different application needs.

Is IronBarcode suitable for both console and web applications?

Yes, IronBarcode is versatile and can be used in both console and .NET MVC web applications, making it a robust choice for various development environments.

What error correction levels are available for QR codes in IronBarcode?

IronBarcode supports four levels of error correction for QR codes: Low, Medium, High, and Highest, ensuring data integrity even if the QR code gets damaged.

What are the licensing options for IronBarcode?

IronBarcode offers a free developer license and a premium version that includes additional support and updates, catering to different development and business needs.

Is IronBarcode compatible with .NET Core and .NET Framework?

Yes, IronBarcode is compatible with both .NET Core and .NET Framework, supporting various architectures including 32- and 64-bit systems.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...Read More