USING IRONBARCODE

How to Generate Barcodes in ASP.NET using C#

Updated January 19, 2024
Share:

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
  • 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

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 also some standard system assemblies, these namespaces are recommended.

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
VB   C#

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");
VB   C#

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();
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")
VB   C#

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()
VB   C#

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.

< PREVIOUS
How to Use Barcode Scanners in C# Windows Application
NEXT >
C# QR Code Reader (Step by Step Tutorial for Beginner)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,246,703 View Licenses >