Skip to footer content
USING IRONBARCODE

How to Generate Barcodes in ASP.NET using C#

This tutorial will demonstrate how to generate a barcode in C# ASP.NET using the IronBarcode library as an example. With this .NET library, it's easy to generate a barcode, style it, and export it as an image, PDF, or HTML.

How to Generate Barcodes in C# .NET Applications

  • Create a Console .NET project in Microsoft Visual Studio
  • Install the barcode library
  • Import namespaces from the barcode package
  • Generate barcode images
  • Barcode image styling
  • Fluency in barcode generation

1. Create a Console .NET Application in Microsoft Visual Studio

This tutorial uses the latest version of Visual Studio and the Console Application (.NET Core) template. It is also compatible with Windows Forms and 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.

How to Generate Barcodes in ASP.NET using C#, Figure 1: Create Console App Create Console App

2. Install the Barcode Generator Library in C#

The IronBarcode library can be installed 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

How to Generate Barcodes in ASP.NET using C#, Figure 2: Package Manager Console installation step Package Manager Console installation step

2. NuGet Package Manager Solution

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

Click on Tools > NuGet Package Manager > Manage NuGet Packages for 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 barcodes to generate barcodes.

How to Generate Barcodes in ASP.NET using C#, Figure 3: Barcode search Barcode search

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

3. Importing NameSpaces

To ensure the class files reference the IronBarcode library and some standard system assemblies, use these namespaces.

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
$vbLabelText   $csharpLabel

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 viewer
System.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 viewer
System.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 viewer
System.Diagnostics.Process.Start("BarCode.png")
$vbLabelText   $csharpLabel

The above code generates barcodes and the output is as follows:

How to Generate Barcodes in ASP.NET using C#, Figure 4: Create a barcode image in C# example Create a barcode image in C# example

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();
barcode.SetMargins(100);
barcode.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();
barcode.SetMargins(100);
barcode.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()
barcode.SetMargins(100)
barcode.ChangeBarCodeColor(Color.Green)
' Save as HTML
barcode.SaveAsHtmlFile("MyBarCode.html")
$vbLabelText   $csharpLabel

How to Generate Barcodes in ASP.NET using C#, Figure 5: Use C# to create an annotated and styled Barcode image Use C# to create an annotated and styled Barcode image

The code should be self-explanatory; however, the GeneratedBarcode class documentation within the API Reference can provide additional technical information.

Additionally, IronBarcode also supports reading barcodes from images, as well as providing extra options to read barcodes with more accuracy or apply filters to images.

6. Fluency in Barcode Generation

IronBarcode implements an optional Fluent API similar to System.Linq for chaining method calls in the following order: create a barcode, set its margins, and then export it to a 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()
$vbLabelText   $csharpLabel

The result is a System.Drawing.Image of a PDF417 barcode, which looks like this:

How to Generate Barcodes in ASP.NET using C#, Figure 6: Simple, Fluent barcode generation in C# using IronBarcode Simple, Fluent barcode generation in C# using IronBarcode

7. Summary

IronBarcode 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. Visit the official document page for more information about IronBarcode.

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two.

Frequently Asked Questions

How do I set up a Console .NET Application to generate barcodes in ASP.NET?

Open Visual Studio, create a new project by selecting 'Console App (.NET)', name your project, choose the target .NET Framework, and click 'Create'. This setup can be used in both Windows Forms and ASP.NET Web Applications.

What are the methods to install the IronBarcode library in a .NET project?

You can install IronBarcode via the Package Manager Console using the command Install-Package IronBarCode, through the NuGet Package Manager, or by downloading the IronBarCode DLL from the official website and adding it to your project references.

Which namespaces are necessary for generating barcodes in C#?

To generate barcodes using IronBarcode, import the following namespaces: using IronBarCode;, using System;, using System.Drawing;, and using System.Linq;.

How can I create a barcode image using C# in ASP.NET?

Use the BarcodeWriter class from IronBarcode. For example: GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://example.com", BarcodeWriterEncoding.Code128); barcode.SaveAsPng("Barcode.png");.

What styling options are available for barcode images using IronBarcode?

IronBarcode allows you to customize barcodes by adding annotations, adjusting font and margins, changing colors, and saving images in different formats such as HTML, PDF, or image files.

How do I use Fluent API in IronBarcode for barcode generation?

IronBarcode's Fluent API allows you to chain methods for creating a barcode, setting margins, and exporting it as a Bitmap in a single line, enhancing code readability and efficiency.

What types of barcodes can be generated using IronBarcode?

IronBarcode supports various barcode types including Code128, QRCode, and PDF417, suitable for different encoding needs in your applications.

Can IronBarcode read barcodes from existing images?

Yes, IronBarcode can read barcodes from images and offers settings to improve reading accuracy, making it versatile for different barcode scanning scenarios.

How can I export barcodes to formats other than images?

Besides exporting to image files, you can use IronBarcode to export barcodes to HTML or PDF formats, providing flexibility in how barcodes are used and shared.

Where can I find detailed documentation for IronBarcode?

Comprehensive documentation and information about IronBarcode can be found on the official Iron Software website, helping you to leverage its full capabilities.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...Read More