In this tutorial, we will learn how to generate QR Codes and Barcodes using C# Language in a dot Net Framework. We will cover the following topics:
- What is a Barcode.?
- What is QR Code.?
- Introduction to a Barcode Library
- Step-by-Step Guide to generate a QR code.
- Create Visual Studio Project
- Download Barcode Library
- Download using Package Manager Console
- Download using Package Manager Solution
- Download using direct link
- Write Code to generate Barcodes
- Write Code to generate QR codes
- Adding a logo to the QR Code
- Styling a QR Code
- Supported QR Code and Barcode Format
A barcode is a set of lines of different widths and sizes representing data that identifies a scannable object. Barcodes are often used to help organize and index information or pricing for a given item.
Barcodes are generally used to help track inventory, but they are also used on admission tickets in movie theaters and other events to uniquely identify and verify the validity of the ticket before the customer can enter. They’re also used to count sales generated by the event, which in turn makes it a lot more convenient to keep track of revenue.
QR Code stands for Quick Response Code, a type of barcode that consists of a printed square pattern of small black and white squares that encode data which can be scanned into a computer system. The black and white squares can represent numbers from 0 to 9 and letters from A to Z, and are typically used for storing URLs or other information that can be read by a smartphone.
QR codes are frequently used to track information about products in a supply chain and – because many smartphones have built-in QR readers – they are often used in marketing and advertising campaigns.
The Iron Barcode Library written in C# provides functions that help us to create QR codes and barcodes with just a single line of code. It also enables us to save the QR code or barcode in our desired file format. We will use the Barcode Library to create QR codes.
I am using Visual Studio 2019 and the console application template for this demonstration. You can use any of your choice.
Open Visual Studio = > Click on Create New Project = > Select Console Application Template = > Press Next => Name the Project => Press Next => Select your Target dot Net framework => Click on Create Button.
Before we begin, we need to install the Barcode Library. You can install it by using one of the following three methods:
You can also install the Barcode Library by using the Nuget Package Solution. Simply follow these steps:
Click on Tools => Nuget Package Manager => Manage Nuget package Solution.
As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.Net Barcode DLL].
Write the following code:
GeneratedBarcode barcode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/docs/", BarcodeEncoding.Code128); barcode.SaveAsPng("barcode.png");
GeneratedBarCode is the object type for storing barcode. BarcodeWriter.CreateBarcode is the function provided by the IronBarCode, and the link in the parameter is the text which we want to encode. I have added a link for the example, but you can add any as per your requirements.
In the second Parameter, we have to set the Encoding Scheme; for barcodes we select Code128.
After that, the SaveAsPng function will save your barcode as a png file in your storage device, as you can see. The IronBarCode provides several file formats for saving your barcode image, such as pdf, gif, png, Html, jpeg, image and WindowsBitmap, etc.
We can see that barcodes can be generated with just a single line of code.
When generating QR codes using Iron Barcode, we may opt to use the QR Code Writer class instead of the BarcodeWriter class. This class gives some new and interesting features for QR code writing. It allows us to set the QR error-correction level, allowing you to decide on a balance between the size of your QR code and its ease of readability.
GeneratedBarcode Qrcode = IronBarCode.QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/docs/"); Qrcode.SaveAsPng("QrCode.png");
Next I will show you how we can add a company logo to the QR Code. This practice is now in everyday use.
Write the following code:
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "CSharp-logo.png", 500); MyQRWithLogo.SaveAsPng("Qr-Code-with-logo.png");
This example adds the C# logo to the QR code. It automatically sizes it to an appropriate size where the pure code is still readable, and aligns the logo to the QR code square grid so that it looks appropriate. The next line of code colors the barcode dark green. However, it does not discolor the logo.
The visual depiction of a QR code or a Barcode can be manipulated very easily using Iron Barcode. The output of every BarcodeWriter operation is a generated barcode object. This generated barcode object has a Fluent API, allowing the graphical settings of the barcode to be set in a single line of code, similar to Linq.
Popular features for styling barcodes include resizing barcodes, setting margins, changing background colors, changing barcode colors, and verifying that the output barcode is still readable.
In a similar fashion, we may also add annotations such as text to a barcode in any typeface we want, or place the value of the barcode very easily under or above the barcode.
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "CSharp-logo.png", 500); MyQRWithLogo.AddAnnotationTextAboveBarcode("Product URL:"); MyQRWithLogo.AddBarcodeValueTextBelowBarcode(); MyQRWithLogo.SaveAsPng("Qr-Code-with-Style.png");
Iron Barcode supports a large number of commonly used barcode formats:
- QR codes, including decorated and branded QR codes with logos and colors
- Multi-format barcodes, such as Aztec, Data Matrix, CODE 93, and CODE 128
- RSS Expanded Databar, UPS MaxiCode, and USPS IMb OneCode barcodes
- Stacked linear barcodes such as RSS-14 and PDF-417
- Conventional numerical barcode formats such as UPCA, UPCE, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey
var MyBarCode = QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/"); MyBarCode.SaveAsImage("MyBarCode.png"); MyBarCode.SaveAsGif("MyBarCode.gif"); MyBarCode.SaveAsHtmlFile("MyBarCode.html"); MyBarCode.SaveAsJpeg("MyBarCode.jpg"); MyBarCode.SaveAsPdf("MyBarCode.Pdf"); MyBarCode.SaveAsPng("MyBarCode.png"); MyBarCode.SaveAsTiff("MyBarCode.tiff"); MyBarCode.SaveAsWindowsBitmap("MyBarCode.bmp");
Iron Barcode features a friendly API for developers to read and write barcodes for .NET, optimizing accuracy and ensuring a low error rate in real-world-use cases. For more information about the Iron Barcode, please visit this link.
Currently, if you buy the complete Iron Suite, you can get five libraries for the price of two. For more details, please click here.