Barcode Generator .NET Tutorial

Barcodes are now used by almost all businesses in today's fast-paced world. Barcodes are typically applied to products to enable quick identification. Among their many other uses, barcodes are usually used in retail stores as a part of purchasing processes, in warehouses to track and manage inventory, and on invoices to help with accounting.

Given the swift rise in barcode usage, developers must be able to generate barcodes in their preferred programming language.

So, in this tutorial, we will learn about generating barcodes in .NET.

We will cover the following topics:

Barcode generator .NET Tutorial

  1. Creating a project in Visual Studio
  2. Installing the C# Barcode Generator Library
  3. Designing the UI of our app
  4. Writing the code for core functionality.
  5. Run our .NET barcode generator Let's begin our tutorial.

Create the Project

I am using Visual Studio 2019 and the Windows Form application template for this demonstration. You can use the application of your choice and use your own existing project and version.

Open Visual Studio = > Click on Create New Project = > Select Windows Form Application Template = > Press Next => Name the Project => Press Next => Select your Target dot Net framework => Click on Create Button.

Installing the Barcode Library

Before installing the Barcode library, I would like to explain why I am using it to generate barcodes.

The Iron Barcode written in C# provides functions that help us to create barcodes and QR codes with just a single line of code. It also enables us to save the QR code or barcode in our desired file format. Further, it provides free service and runtime support for generating barcodes in .NET. This will all help us with generating barcodes.

Before we begin, we need to install the Barcode NuGet Package. You can install it by using one of the following three methods:

Package Manager Console

Write the following command in the Package Manager console. It will download and install the package for you.

Install-Package Barcode

NuGet Package Manager Solution

You can also install the Barcode Package by using the NuGet Package Solution. Simply follow these steps:

Click on Tools => NuGet Package Manager => Manage NuGet package Solution.

This will open NuGet Package Manager for you. Click on Browse and search BarCode, then install the library.

As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from .NET Barcode DLL.

Design the Windows Form

Now, we will design UI for our .NET Barcode generator. Pick 2 labels, 1 Rich Textbox, 1 Picture Box to display the generated barcode image.

Design the form as per your choice. For demonstration purposes, I have kept the design simple.

Write Code for Generating Barcode:

Double click the "Generate" button. The following code will appear:

private void button1_Click(object sender, EventArgs e)
private void button1_Click(object sender, EventArgs e)
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
VB   C#

Add the following namespace

using IronBarCode;
using System;
using System.Drawing;
using System.Windows.Forms;
using IronBarCode;
using System;
using System.Drawing;
using System.Windows.Forms;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Windows.Forms
VB   C#

Write the following code inside the button1_Click() function:

GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
BarcodeImage.Image = new Bitmap("MyBarCode.png");
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
BarcodeImage.Image = new Bitmap("MyBarCode.png");
Dim MyBarCode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128)
BarcodeImage.Image = New Bitmap("MyBarCode.png")
VB   C#

Let's understand the code, line by line.

Generated barcode is a data type of barcode. Create barcode is the function provided by the BarcodeWriter Class of IronBarcode.

BarcodeValue.Text is the value of the Textbox which will be provided by the user on run time.

BarcodeWriterEncoding.Code128 is the encoding scheme used for generating barcodes. If we want to generate a QR code we just have to change the encoding scheme to BarcodeWriterEncoding.qrCode.

IronBarcode provides us with multiple image formats such as PNG, JPEG, HTML, PDF, GIF, and many more in which to save our created barcodes. I have used SaveAsPng() to save my barcode in PNG format.

BarcodeImage is the name of the PictureBox which we have used to show our generated barcode images to the user.

Run our .NET Barcode Generator

Press Ctrl + F5 to run the application.

Write the value inside the text box which you want to encode in the barcode as shown below.

Now, click the "Generate" button. The barcode will be generated as shown below.

Display the Value of the Barcode

Next, let's assume we want to show the value of the barcode.

Add the following line of code:

VB   C#



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 software. For more information about 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 information, please click here.

You can download a file project from this link.