USING IRONBARCODE

.NET QR Code Generator (Code Example Tutorial)

Updated February 7, 2024
Share:

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 > COM 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 COM 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:

QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png");
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png");
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("MyQR.png")
VB   C#

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")
VB   C#

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"))
VB   C#

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")
VB   C#

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()
{
    QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png");
    return File("Demo.png", "image/png", "Demo.png", true);
}
public IActionResult Index()
{
    QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png");
    return File("Demo.png", "image/png", "Demo.png", true);
}
Public Function Index() As IActionResult
	QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPdf("Demo.png")
	Return File("Demo.png", "image/png", "Demo.png", True)
End Function
VB   C#

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.

< PREVIOUS
C# Barcode Scanner Step-by-Step Tutorial
NEXT >
How to Generate QR Code in C# Windows Applications

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,246,703 View Licenses >