Generate Barcode Images in C#
In this tutorial, we will see how to generate a barcode in C# .NET with an example using the Iron Barcode library.
We will see how easy it is to create a barcode in C# or VB.NET, as well as how to style our barcode, and then export it as an image.
Get started with IronBarcode
Start using IronBarcode in your project today with a free trial.
How to Generate Barcodes in C# .NET
- Download IronBarcode using the DLL download or NuGet
- Generate a Simple Barcode or QR
- Use Advanced Settings to Style and Customize your Barcode
- Implement Complex Barcodes in a Single Line of Code
- Download this tutorial project
Installation
The first thing we need to do is install the Iron Barcode library, adding barcode functionality to the .NET framework. We can do this using our NuGet package or by downloading the .NET Barcode DLL.
Install-Package BarCode

Render a Simple Barcode
In the following example, we can see that a barcode can be created containing numerical or text content using only a couple of lines of code, using Iron Barcode.
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-1.cs
// Import the IronBarCode library to work with barcodes
using IronBarCode;
// Generate a Simple BarCode image and save it as PNG
// The CreateBarcode method generates a barcode image based on the provided content and encoding
// In this case, a URL is used, and Code128 is chosen as the encoding standard.
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
// Save the generated barcode image as a PNG file.
// "myBarcode.png" is the name of the file that will be created in the working directory.
myBarcode.SaveAsPng("myBarcode.png");
// This line opens the image in your system's default image viewer.
// It uses the Process class to start a new process.
// The image file will be opened using the default image viewing application associated with the .png file type.
// 'UseShellExecute = true' allows the opening of external applications via the shell.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true });
' Import the IronBarCode library to work with barcodes
Imports IronBarCode
' Generate a Simple BarCode image and save it as PNG
' The CreateBarcode method generates a barcode image based on the provided content and encoding
' In this case, a URL is used, and Code128 is chosen as the encoding standard.
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
' Save the generated barcode image as a PNG file.
' "myBarcode.png" is the name of the file that will be created in the working directory.
myBarcode.SaveAsPng("myBarcode.png")
' This line opens the image in your system's default image viewer.
' It uses the Process class to start a new process.
' The image file will be opened using the default image viewing application associated with the .png file type.
' 'UseShellExecute = true' allows the opening of external applications via the shell.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("myBarcode.png") With {.UseShellExecute = True})
We first create the barcode by specifying its value and the barcode format we will be using from the IronBarCode.BarcodeWriterEncoding
Enum. We can then choose to save it as an image or save it as a System.Drawing.Image
or Bitmap
object. That's all the code it takes!

The final line of code simply opens the barcode PNG in the example so that you can see it with your own eyes.
Advanced Barcode Creation
Although the previous example was effective, in the real world we may wish to do more.
In the following example, we may add annotations to the barcode, set the font, display its value below it, add margins, change the barcode color, and then save it, all quite simply in C#.
We can also choose to export to HTML or PDF instead of an image if that is more appropriate for our application.
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-2.cs
using IronBarCode;
using IronSoftware.Drawing;
// This script generates a styled QR code with annotation text
// and saves it as an HTML file.
// Generate a QR code with a specified URL
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode",
BarcodeWriterEncoding.QRCode
);
// Add annotation text above the QR code
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
// Add the barcode value text below the QR code
myBarCode.AddBarcodeValueTextBelowBarcode();
// Set the margin around the barcode
myBarCode.SetMargins(100);
// Change the color of the QR code to purple
myBarCode.ChangeBarCodeColor(Color.Purple);
// Save the QR code as an HTML file
myBarCode.SaveAsHtmlFile("MyBarCode.html");
Imports IronBarCode
Imports IronSoftware.Drawing
' This script generates a styled QR code with annotation text
' and saves it as an HTML file.
' Generate a QR code with a specified URL
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
' Add annotation text above the QR code
myBarCode.AddAnnotationTextAboveBarcode("Product URL:")
' Add the barcode value text below the QR code
myBarCode.AddBarcodeValueTextBelowBarcode()
' Set the margin around the barcode
myBarCode.SetMargins(100)
' Change the color of the QR code to purple
myBarCode.ChangeBarCodeColor(Color.Purple)
' Save the QR code as an HTML file
myBarCode.SaveAsHtmlFile("MyBarCode.html")

The code should be self-explanatory, but if it is not, I encourage you to read the GeneratedBarcode
class documentation within the API Reference .
Fluency
In our final example, we will see that we may create, style, and export a barcode in a single line of code.
Iron Barcode implements an optional Fluent API similar to System.Linq. By chaining method calls to method calls to method calls, we first create a barcode, then set its margins, then export to Bitmap in a single line.
This can be very convenient and make code easier to read.
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-3.cs
using IronBarCode;
using IronSoftware.Drawing; // Using AnyBitmap from the IronSoftware.Drawing namespace
// Define the URL or string value for which you want to generate a barcode
string value = "https://ironsoftware.com/csharp/barcode";
// Create a barcode using the PDF417 encoding format
// Perform a series of fluent operations to modify its appearance
AnyBitmap barcodeBitmap = BarcodeWriter
.CreateBarcode(value, BarcodeEncoding.PDF417) // Encode the value to a barcode format
.ResizeTo(300, 200) // Resize the barcode image to 300x200 pixels
.SetMargins(10) // Add a margin/border of 10 pixels around the barcode
.ToBitmap(); // Convert the barcode to an AnyBitmap object
// Convert the AnyBitmap type to the System.Drawing.Bitmap type,
// which is compatible with legacy Windows forms or GDI+ applications.
System.Drawing.Bitmap barcodeLegacyBitmap = barcodeBitmap.ToBitmap(); // Correctly cast AnyBitmap to System.Drawing.Bitmap
// Now, 'barcodeLegacyBitmap' can be used in applications that require System.Drawing.Bitmap,
// such as displaying in a Windows Form PictureBox or saving to a file in formats like JPEG or PNG.
Imports IronBarCode
Imports IronSoftware.Drawing ' Using AnyBitmap from the IronSoftware.Drawing namespace
' Define the URL or string value for which you want to generate a barcode
Private value As String = "https://ironsoftware.com/csharp/barcode"
' Create a barcode using the PDF417 encoding format
' Perform a series of fluent operations to modify its appearance
Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(10).ToBitmap() ' Convert the barcode to an AnyBitmap object
' Convert the AnyBitmap type to the System.Drawing.Bitmap type,
' which is compatible with legacy Windows forms or GDI+ applications.
Private barcodeLegacyBitmap As System.Drawing.Bitmap = barcodeBitmap.ToBitmap() ' Correctly cast AnyBitmap to System.Drawing.Bitmap
' Now, 'barcodeLegacyBitmap' can be used in applications that require System.Drawing.Bitmap,
' such as displaying in a Windows Form PictureBox or saving to a file in formats like JPEG or PNG.
The result is a System.Drawing.Image
of a PDF417 barcode which looks like this:

Learning More
To learn more about this code sample and how to read images from barcodes in C#, you may wish to view it on GitHub, download it as a Visual Studio Project or look at the other examples within this section, including our tutorial on how to create QR codes.
Source Code Downloads in C#
The source for this "Barcode Image Generation" tutorial is available as a C# barcode generator code project for Visual Studio 2017:
Further Documentation
You may also find the BarcodeReader classes within the API Reference of great value. There is also information about how to use the software as a C# Barcode Scanner.
In addition, there are other tutorials which may shed light on other aspects of IronBarCode including QR codes and Reading Barcode Images with .NET.
Frequently Asked Questions
What is IronBarcode?
IronBarcode is a C# library that allows developers to generate and customize barcode images in .NET applications.
How do I install IronBarcode in a C# project?
You can install IronBarcode in a C# project by using the NuGet package manager with the command 'dotnet add package BarCode' or by downloading the .NET Barcode DLL.
How can I generate a simple barcode using IronBarcode?
To generate a simple barcode, you can use the BarcodeWriter class to create a barcode with specified value and format, then save it as an image using a few lines of C# code.
Can I customize the appearance of barcodes generated with IronBarcode?
Yes, IronBarcode allows you to customize barcodes by adding annotations, changing colors, setting fonts, and adjusting margins to fit your specific needs.
What barcode formats are supported by IronBarcode?
IronBarcode supports various barcode formats including Code128, PDF417, QR Codes, and more, allowing you to implement complex barcodes easily.
Is it possible to create, style, and export a barcode in a single line of code?
Yes, IronBarcode provides a Fluent API that allows you to create, style, and export a barcode in a single line of code, making code more readable and efficient.
What platforms can I export the barcode to?
Barcodes can be exported to image formats like PNG, as well as HTML or PDF, depending on your application needs.
Where can I find the source code for this tutorial?
The source code for the 'Barcode Image Generation' tutorial is available on GitHub and can also be downloaded as a C# project for Visual Studio 2017.
Does IronBarcode support barcode scanning?
Yes, IronBarcode includes classes for reading and scanning barcodes within .NET applications, providing comprehensive functionality for barcode handling.
How can I learn more about other functionalities of IronBarcode?
You can explore further documentation and tutorials on the IronBarcode website, which cover topics such as QR code generation and barcode image reading.