Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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 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
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")
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();
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")
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.
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.
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.
You need to import the following namespaces: 'using IronBarCode;', 'using System;', 'using System.Drawing;', and 'using System.Linq;'.
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");'.
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.
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.
IronBarcode supports a variety of barcode types including Code128, QRCode, and PDF417 among others, which can be used for different encoding scenarios.
Yes, IronBarcode provides functionality to read barcodes from images and offers settings to enhance reading accuracy.
Yes, apart from exporting to image files, you can also export barcodes to HTML or PDF formats using IronBarcode.
You can visit the official document page on the IronSoftware website for comprehensive information about IronBarcode.