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 written 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
using IronBarCode;
// Generate a Simple BarCode image and save as PNG
GeneratedBarcode myBarcode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
myBarcode.SaveAsPng("myBarcode.png");
// This line opens the image in your default image viewer
System.Diagnostics.Process.Start("myBarcode.png");
Imports IronBarCode
' Generate a Simple BarCode image and save as PNG
Private myBarcode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
myBarcode.SaveAsPng("myBarcode.png")
' This line opens the image in your default image viewer
System.Diagnostics.Process.Start("myBarcode.png")
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 as image or save 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;
// Styling a QR code and adding annotation text
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
myBarCode.AddBarcodeValueTextBelowBarcode();
myBarCode.SetMargins(100);
myBarCode.ChangeBarCodeColor(Color.Purple);
// Save as HTML
myBarCode.SaveAsHtmlFile("MyBarCode.html");
Imports IronBarCode
Imports IronSoftware.Drawing
' Styling a QR code and adding annotation text
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
myBarCode.AddAnnotationTextAboveBarcode("Product URL:")
myBarCode.AddBarcodeValueTextBelowBarcode()
myBarCode.SetMargins(100)
myBarCode.ChangeBarCodeColor(Color.Purple)
' Save as HTML
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;
// Fluent API for Barcode Image generation.
string value = "https://ironsoftware.com/csharp/barcode";
AnyBitmap barcodeBitmap = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(100).ToBitmap();
System.Drawing.Bitmap barcodeLegacyBitmap = (System.Drawing.Bitmap)barcodeBitmap;
Imports IronBarCode
Imports IronSoftware.Drawing
' Fluent API for Barcode Image generation.
Private value As String = "https://ironsoftware.com/csharp/barcode"
Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(100).ToBitmap()
Private barcodeLegacyBitmap As System.Drawing.Bitmap = CType(barcodeBitmap, System.Drawing.Bitmap)
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 are 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 in other aspects of IronBarCode including QR codes and Reading Barcode Images with .NET.