Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
Create Console App
The IronBarcode library can be installed using one of the following three methods:
Write the following command in the Package Manager Console. It will download and install the package for you.
Install-Package BarCode
Package Manager Console installation step
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.
Barcode search
As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference.
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
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:
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.
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")
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.
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()
The result is a System.Drawing.Image
of a PDF417
barcode which looks like this:
Simple, Fluent barcode generation in C# using IronBarcode
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.
9 .NET API products for your office documents