DEV Community

Mohammed Ibrahim
Mohammed Ibrahim

Posted on

How to Generate QR Code in C# (Developer Tutorial)

1.0 Introduction

The automotive sector in Japan is where the Quick Response Code, or QR code, (sometimes referred to as the QR Symbol, QR Graphic, or QR Code Graphic) first appeared. It was initially created in 1994 by Masahiro Hara as a practical and effective means of tracking cars while they were assembled in the plant. Since then, barcodes have been replaced with QR codes in a wide range of industries beyond the automobile manufacturing sector. The reason for this is its exceptional reading speed and storage capacity. QR codes are useful for tracking and identifying products, but they may also be used to store and distribute information for marketing campaigns.

1.1 Introducing IronBarcode

Making a QR code is easy using IronBarcode's QR Code Library. This simple library can generate a QR code in a few lines of code.
Additional Features

  • IronBarcode can read and write most barcode formats, including UPC A/E, EAN 8/13, Code 39/93/128, ITF, MSI, RSS 14/Expanded, Databar, and CodaB.
  • Barcode pictures are automatically preprocessed by IronBarcode to improve reading efficiency and accuracy.
  • IronBarcode can read scans and real-time video frames to rectify skewing, noise, distortion, and rotation.
  • IronBarcode allows for the use of several cores and threads, which is particularly useful for server applications that handle data in batches.
  • IronBarcode can automatically find one or more barcodes in documents with one or more pages.
  • IronBarcode supports 32- and 64-bit architectures and is compatible with.NET Core and.NET Framework implementations.
  • IronBarcode supports console, desktop, cloud, and online applications on PC and mobile platforms.
  • For a variety of file and stream types, including PDF, JPG, TIFF, GIF, BMP, PNG, and HTML, IronBarcode can create QR code graphics.

2.0 How to Create QR Code in C

  1. Using Microsoft Visual Studio, create a Windows Forms application.
  2. Setting up the library for QR codes
  3. Creating barcodes via importing namespaces
  4. Using a single line of code to create a QR code
  5. Enhancing a QR code image with a logo

2.1 Generating QR Codes Using IronBarcode

The examples in the following sections demonstrate how easy it is for our library to generate QR codes.
Start a new project first.
After starting Visual Studio, select File > New Project.
Choose the Console App template from the window that appears, then click Next.

Image description
In the Project name text field, type any project name you choose (e.g., QR Code Generator). In the Location field, enter the new project's location. Next, click the Next button to continue.

Image description
Click Create after choosing a.NET Framework (in this case 6.0 (Long term support)) from the Framework dropdown menu.

Image description
Creating the new Console App in Visual Studio under .NET 6.0 Framework

2.2 Install the Barcode Library

2.2.0 Using IronBarcode

There are four ways to get and install the IronBarcode library.
Those methods are:

  • Utilizing the NuGet Package Manager UI in Visual Studio.
  • Through the Package Manager Console in Visual Studio.
  • obtaining a direct download using the NuGet website (or)
  • obtaining a direct download from the IronBarcode website.

2.2.1 Utilizing the NuGet Package Manager UI in Visual Studio

To access the Package Manager UI, select Tools > NuGet Package Manager > Manage NuGet Packages for solution... from the menu bar.

Image description
As an alternative, you can use the context menu to manage NuGet Packages... by right-clicking the project name in the Solution Explorer Window.
Select the Browse tab and fill in the search area with Barcode. Click the Install button after selecting IronBarcode from the associated packages list (which appears as the first result in the image below), selecting your project in the pane on the right.

Image description

2.2.2 Through the Package Manager Console in Visual Studio

Navigate to the Package Manager Console under Tools -> NuGet Package Manager. In the Command-line panel that displays, type the following command and hit ENTER:

Install-Package BarCode
Enter fullscreen mode Exit fullscreen mode

The aforementioned command will install our library into the running project and download it.

2.2.3 obtaining a direct download using the NuGet website

Use your browser to look for our Barcode library page on the NuGet Gallery website (you can just click here to go straight to the page).
To save the library to your computer, select the Download package option from the menu on the right. After that, double-click the downloaded library in your File Manager to add it instantly to your project. When you finally reload your project, it will be operational.

2.2.4 obtaining a direct download from the IronBarcode website

To obtain the most recent.NET barcode DLL from our website, click this link. After downloading, add the package to your project by following the instructions below:

  1. From the Solution Explorer Panel, right-click the project and select Add > COM Reference.

Image description

  1. After extracting the DLL, click the Browse button and find the location there. Click OK to include the DLL in your project when it has been selected.

Image description

3.0 Generate a QR Code Image

3.1 Using IronBarcode from Windows/Console Application

To create a new QR code, use the QRCodeWriter.CreateQrCode technique:

QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium,0).SaveAsPng("MyQR.png");
Enter fullscreen mode Exit fullscreen mode

The data to be encoded in the code image (which can be either a String or a Stream) is the only mandatory parameter for the createQrCode function. Three additional parameters are accepted by the method:

  1. The graphic's dimensions (by default, 500 pixels by 500 pixels)
  2. A level of mistake correction. Error correction is available in four levels with IronBarcode: Low, Medium, High, and Highest. The maximum correction level (QRCodeWriter.QrErrorCorrectionLevel.Highest) is what createQrCode uses by default.
  3. A version number for the QR code. For a list of approved versions, visit this page. The method is instructed to use the correct version number based on the data it will encode when the value, which is 0 by default, is entered. The 500 × 500-pixel image produced by the aforementioned example uses a medium level of error correction. The created QR code is then saved as a PNG file at a specified file location by making a further call to the SaveAsPng function.

Image description

3.2 Using IronBarcode With Logo image

We'll then look at a common use-case scenario when a user or business wishes to add their logo to a QR code that has been generated. The QrCodeWriter is used in the sample code below. Use the CreateQrCodeWithLogo function to accomplish this:

var QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("Hello World",                       "qrlogo.png",500);
QRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkRed);
QRWithLogo.SaveAsPng("Logo_QR_Code.png");
Enter fullscreen mode Exit fullscreen mode

In the previous example, we create a new QR code that embeds an image from a specified file location by encoding a String value of "Hello, World".
The output of the source code above is displayed in the image below.

QRWithLogo.SaveAsHtmlFile("test.html");
Enter fullscreen mode Exit fullscreen mode

There are images in the QR code above. In order to preserve the readability of the pure code, the logo is automatically resized and aligned to suit the QR code square grid. While the other makes use of color-class types, the first makes use of HTML hex color notation. We may also change the color of the QR code by using the changebarcodecolor tool, which offers a range of code colors we may apply to the code. In the line of code that follows, the barcode is colored dark green. and after that, as the code indicates, we save them as an HTML or PNG file.
The image below displays the source code's output.

Image description

To learn more about the Qr generate refer to the link https://ironsoftware.com/csharp/barcode/tutorials/csharp-qr-code-generator/.

4.0 Conclusion

One of the most effective libraries for creating and recognizing barcodes is IronBarcode. It is also among the quickest barcode generation and recognition libraries available. There are multiple operating systems that support this library. It is simple to design and supports a large range of barcode formats. We can alter the barcode text, width, height, color, and other elements. You may find the licensing information from here https://ironsoftware.com/csharp/barcode/licensing/. There is a premium edition of Iron Barcode as well as a free developer license. The lite version starts at $749 which includes free updates and support for a year.

Top comments (0)