USING IRONBARCODE

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 create a Console .NET Application for generating barcodes?

Open Visual Studio, click on 'Create New Project', select 'Console App (.NET)', proceed with naming the project, selecting the target .NET Framework, and clicking the 'Create' button. Then, design the form using Label, TextBox, and Button controls.

What are the methods to install the IronBarcode library?

You can install IronBarcode using the Package Manager Console with the command 'Install-Package IronBarCode', via the NuGet Package Manager solution, or by downloading the IronBarCode.Dll from the official website and adding it as a project reference.

Which namespaces are required for generating barcodes using IronBarcode?

You need to import the following namespaces: 'using IronBarCode;', 'using System;', 'using System.Drawing;', and 'using System.Linq;'.

How can I generate a simple barcode image in C#?

Use the BarcodeWriter class to create a barcode and save it as a PNG file. For example, 'GeneratedBarcode barCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128); barCode.SaveAsPng("BarCode.png");'.

What options does IronBarcode provide for styling barcode images?

IronBarcode allows you to add annotations, set font and margins, change barcode colors, and save images in various formats like HTML, PDF, or image files.

How can I use the Fluent API for barcode generation in IronBarcode?

The Fluent API allows chaining methods for creating a barcode, setting margins, and exporting it as a Bitmap in a single line, making the code more readable.

What types of barcodes can be generated with IronBarcode?

IronBarcode supports a variety of barcode types including Code128, QRCode, and PDF417 among others, which can be used for different encoding scenarios.

Can IronBarcode read barcodes from images?

Yes, IronBarcode provides functionality to read barcodes from images and offers settings to enhance reading accuracy.

Is it possible to export barcodes to formats other than images?

Yes, apart from exporting to image files, you can also export barcodes to HTML or PDF formats using IronBarcode.

Where can I find more information about IronBarcode?

You can visit the official document page on the IronSoftware website for comprehensive information about IronBarcode.

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 says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Use Barcode Scanners in C# Windows Apps
NEXT >
C# QR Code Reader (Step by Step Tutorial for Beginner)