How to Generate Barcode Images in C# for .NET using Iron Barcode

In this tutorial, learn how to generate barcode images using C# and the Iron Barcode library. Begin by opening Visual Studio and accessing the package manager console. Install the barcode library using the command Install-Package Barcode. Once installed, navigate to the Program.cs file to write your code.

First, you need to import the IronBarcode namespace, which provides all the necessary classes and methods to create and manage barcodes. Use BarcodeWriter.CreateBarcode() to generate the barcode, specifying the text you wish to encode. In this example, the text is 'This is my first barcode by Iron Barcode'. The encoding scheme used here is BarcodeEncoding.Code128. Finally, save the barcode as an image file. You can choose from formats like PNG, JPEG, and PDF. Here, we'll save it as 'barcode.png'.

Below is the example code in C#:

using System;
using IronBarCode;  // Import the IronBarcode namespace

class Program
{
    static void Main()
    {
        // Define the text to encode in the barcode
        string textToEncode = "This is my first barcode by Iron Barcode";

        // Create a barcode using the specified encoding type
        var barcode = BarcodeWriter.CreateBarcode(textToEncode, BarcodeEncoding.Code128);

        // Save the barcode as a PNG image
        // The image will be saved in the project directory
        barcode.SaveAsPng("barcode.png");

        // Optionally, add the encoded value text below the barcode
        barcode.AddBarcodeValueTextBelowBarcode();

        // Save the updated barcode with text below as a PNG image
        barcode.SaveAsPng("barcode_with_text.png");

        Console.WriteLine("Barcode generated and saved successfully.");
    }
}
using System;
using IronBarCode;  // Import the IronBarcode namespace

class Program
{
    static void Main()
    {
        // Define the text to encode in the barcode
        string textToEncode = "This is my first barcode by Iron Barcode";

        // Create a barcode using the specified encoding type
        var barcode = BarcodeWriter.CreateBarcode(textToEncode, BarcodeEncoding.Code128);

        // Save the barcode as a PNG image
        // The image will be saved in the project directory
        barcode.SaveAsPng("barcode.png");

        // Optionally, add the encoded value text below the barcode
        barcode.AddBarcodeValueTextBelowBarcode();

        // Save the updated barcode with text below as a PNG image
        barcode.SaveAsPng("barcode_with_text.png");

        Console.WriteLine("Barcode generated and saved successfully.");
    }
}
Imports System
Imports IronBarCode ' Import the IronBarcode namespace

Friend Class Program
	Shared Sub Main()
		' Define the text to encode in the barcode
		Dim textToEncode As String = "This is my first barcode by Iron Barcode"

		' Create a barcode using the specified encoding type
		Dim barcode = BarcodeWriter.CreateBarcode(textToEncode, BarcodeEncoding.Code128)

		' Save the barcode as a PNG image
		' The image will be saved in the project directory
		barcode.SaveAsPng("barcode.png")

		' Optionally, add the encoded value text below the barcode
		barcode.AddBarcodeValueTextBelowBarcode()

		' Save the updated barcode with text below as a PNG image
		barcode.SaveAsPng("barcode_with_text.png")

		Console.WriteLine("Barcode generated and saved successfully.")
	End Sub
End Class
$vbLabelText   $csharpLabel

In the above code:

  • We first define the text that we want to encode into the barcode.
  • We create a barcode using the BarcodeWriter.CreateBarcode() method, specifying the text and the encoding type BarcodeEncoding.Code128.
  • The generated barcode is then saved as a PNG file using the SaveAsPng() method.
  • We optionally add the barcode value as text below the barcode using AddBarcodeValueTextBelowBarcode() and save the updated barcode as another PNG file.

Run the project to generate the image, which can be found in the bin folder of your project directory. If you encounter any issues, feel free to contact our support team for assistance.

Further Reading: Generate Barcode Images in C#

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Read Barcodes in C# for .NET 5 using Iron Barcode
NEXT >
How to Read Multiple Barcodes at Once in C#