Published May 3, 2022
How to Generate Barcodes in ASP.NET using C#
In this tutorial we will look at how to generate a barcode in C# ASP.NET with an example using the Iron Barcode library.
We will see how easy it is to generate a barcode, as well as how to style our barcode and then export it as an image, PDF or HTML. Let's first clarify what barcode images are.
What are Barcodes?
A barcode is a square or rectangular image consisting of a series of parallel black lines and white spaces of varying widths that can be read by a scanner. Barcodes are often used to help organize and index information or prices about an object.
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.
How to Generate Barcodes in C# .NET Applications
- Create a Console .NET project in Microsoft Visual Studio
- Install the Barcode library
- Importing namespaces from the Barcode folder
- Generate Barcode Images
- Barcode Image Styling
- Fluency in Barcode Generation
1. Create a Console .NET Application in Microsoft Visual Studio
I am using Visual Studio 2019 and the console application (.net core) template for this demonstration. You can also use Windows Forms or ASP.NET web applications.
Open Visual Studio = > Click on Create New Project = > Select Console App (.NET) = > Press Next => Name the Project => Press Next => Select your target .NET framework => Click on the Create Button.
After creating the project, design the form as follows from the Visual Studio toolbox: Label, Textbox and Button controls.

2. Install the Barcode Generator Library in C#
Before we begin, we need to install the Barcode library. You can install it by using one of the following three methods:
1. Package Manager Console
Write the following command in the Package Manager console. It will download and install the package for you.
Install-Package BarCode

2. NuGet Packages Manager Solution
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.
This will open the NuGet Package Manager for you. Click on Browse and search for Barcode, then install the library. Or, you can click on "add project reference" in solution explorer to add the class library for Barcode to generate barcodes.

3. Download from the Link
As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.NET Barcode DLL].
3. Importing NameSpaces
For this tutorial, we need to make sure our class files reference IronBarCode and also some normal system assemblies.
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
4. Generate Barcode Images
In the following sample code, you can create barcode images containing numerical or text content using only one line of code. You can also save them as PNG image files and view them in your application.
// Generate a Simple BarCode image and save as PNG
GeneratedBarcode BarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
BarCode.SaveAsPng("BarCode.png");
// This line opens the image in your default image viewerSystem.Diagnostics.Process.Start("BarCode.png");
// Generate a Simple BarCode image and save as PNG
GeneratedBarcode BarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
BarCode.SaveAsPng("BarCode.png");
// This line opens the image in your default image viewerSystem.Diagnostics.Process.Start("BarCode.png");
' Generate a Simple BarCode image and save as PNG
Dim BarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
BarCode.SaveAsPng("BarCode.png")
' This line opens the image in your default image viewerSystem.Diagnostics.Process.Start("BarCode.png");
The above code generates barcodes and the output is as follows:

The last line of code simply opens the barcode PNG in the default image viewer so that you can see it in the barcode generator output.
5. Barcode Image Styling
In the following sample code, you will see how annotations are added to the barcode. You can set the font, display its value below it, add margins, change the barcode color, and then save it, all quite simply in C#. Finally, you can easily save it to various image files.
You can also choose to export to HTML or PDF instead of an image if that is more appropriate for your application.
// Styling a QR code and adding annotation text
var BarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
BarCode.AddAnnotationTextAboveBarcode("Product URL:");
BarCode.AddBarcodeValueTextBelowBarcode();MyBarCode.SetMargins(100);MyBarCode.ChangeBarCodeColor(Color.Green);
// Save as HTML
BarCode.SaveAsHtmlFile("MyBarCode.html");
// Styling a QR code and adding annotation text
var BarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
BarCode.AddAnnotationTextAboveBarcode("Product URL:");
BarCode.AddBarcodeValueTextBelowBarcode();MyBarCode.SetMargins(100);MyBarCode.ChangeBarCodeColor(Color.Green);
// Save as HTML
BarCode.SaveAsHtmlFile("MyBarCode.html");
' Styling a QR code and adding annotation text
Dim BarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
BarCode.AddAnnotationTextAboveBarcode("Product URL:")
BarCode.AddBarcodeValueTextBelowBarcode()
MyBarCode.SetMargins(100)
MyBarCode.ChangeBarCodeColor(Color.Green)
' Save as HTML
BarCode.SaveAsHtmlFile("MyBarCode.html")

The code should be self-explanatory, but if it is not, I encourage you to read the GeneratedBarcode class documentation within the API Reference.
6. Fluency in Barcode Generation
Iron Barcode implements an optional Fluent API similar to System.Linq. By chaining method calls to method, we first create a barcode, then set its margins, then export it to Bitmap in a single line.
This can be very convenient and make code easier to read.
// Fluent API for Barcode Image generation.
string MyValue = "https://ironsoftware.com/csharp/barcode";
Bitmap BarcodeBmp = BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap();
// Fluent API for Barcode Image generation.
string MyValue = "https://ironsoftware.com/csharp/barcode";
Bitmap BarcodeBmp = BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap();
' Fluent API for Barcode Image generation.
Dim MyValue As String = "https://ironsoftware.com/csharp/barcode"
Dim BarcodeBmp As Bitmap = BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap()
The result is a System.Drawing.Image of a PDF417 barcode which looks like this:

7. Summary
Iron Barcode features a friendly API for developers to read and generate barcode images and QR codes for C# .NET, optimizing accuracy and ensuring a low error rate in real-world-use cases. You can also print barcode images. For more information about IronBarcode, please visit this link.
Currently, if you buy the complete Iron Suite you can get five libraries for the price of just two. For more details, please click here.