Generate Barcode Images in C#
Published: 2021-12-21 04:07:30

---
canonical_url: https://ironsoftware.com/csharp/barcode/tutorials/csharp-barcode-image-generator/
---

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.

# How to Generate Barcodes in C# .NET

1. Download IronBarcode using the <a href="https://ironsoftware.com/csharp/barcode/packages/IronBarCode.zip" target="_blank">DLL download</a> or <a href="https://www.nuget.org/packages/barcode" target="_blank">NuGet</a>
2. Generate a Simple Barcode or QR
3. Use Advanced Settings to Style and Customize your Barcode
4. Implement Complex Barcodes in a Single Line of Code
5. Download this tutorial project

{% youtube pBPD7PD0-Xw %}

## 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 <a href="https://www.nuget.org/packages/barcode" target="_blank">NuGet package</a> or by downloading the <a href="https://ironsoftware.com/csharp/barcode/packages/IronBarCode.zip" target="_blank">.Net Barcode DLL</a>

`PM > Install-Package Barcode`

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5aseynzywl02w2catfd.jpg)

## 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.

**C#:**
```cs
// Generate a Simple BarCode image and save as PNG
//using IronBarCode;
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");
```
**VB:**
```vb
' Generate a Simple BarCode image and save as PNG
'using IronBarCode;
Dim 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!

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/el18sjln0l7z099i4w02.png)

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.

**C#:**
```cs
//using IronBarCode;
//using System.Drawing;
 // Styling a QR code and adding annotation text
var MyBarCode = IronBarCode.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");  
```
**VB:**
```vb
'using IronBarCode;
'using System.Drawing;
 ' Styling a QR code and adding annotation text
Dim MyBarCode = IronBarCode.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")
```

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9h8hfvla0udq5rgdh82.png)

The code should be self-explanatory, but if it is not, I encourage you to read the <a href="https://ironsoftware.com/csharp/barcode/object-reference/api/IronBarCode.GeneratedBarcode.html" target="_blank">GeneratedBarcode</a> class documentation within the <a href="https://ironsoftware.com/csharp/barcode/object-reference/" target="_blank">API reference</a>

## 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.

**C#:**
```cs
//using IronBarCode;
//using System.Drawing;
 // Fluent API for Barcode Image generation.
string MyValue = "https://ironsoftware.com/csharp/barcode";
Bitmap BarcodeBmp = IronBarCode.BarcodeWriter.CreateBarcode(MyValue, BarcodeEncoding.PDF417).ResizeTo(300,200).SetMargins(100).ToBitmap();
```
**VB:**
```vb
'using IronBarCode;
'using System.Drawing;
 ' Fluent API for Barcode Image generation.
Dim MyValue As String = "https://ironsoftware.com/csharp/barcode"
Dim BarcodeBmp As Bitmap = IronBarCode.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:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbubjtm0saxj7x4ubr5b.png)

## Learning More

To learn more about this code sample and how to <a href="https://ironsoftware.com/csharp/barcode/use-case/read-barcode-image-csharp/" target="_blank">read images from barcodes in C#</a>, 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:

- <a href="https://github.com/iron-software/Iron-Barcode-CSharp-Barcode-Image-Generator-Tutorial" target="_blank" rel="nofollow">GitHub Repository </a>
- C# Source Code Zip,/a>

### Further Documentation

You may also find the <a href="https://ironsoftware.com/csharp/barcode/object-reference/api/IronBarCode.BarcodeWriter.html" target="_blank">BarcodeReader</a> classes within the API Reference of great value. There is also information about how to use the software as a <a href="https://ironsoftware.com/csharp/barcode/use-case/barcode-scanner-csharp/" target="_blank">C# Barcode Scanner</a>.

In addition, there are other tutorials which may shed light in other aspects of IronBarCode including _QR codes_ and _Reading Barcode Images with .Net_.