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
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
In the above code:
BarcodeWriter.CreateBarcode()
method, specifying the text and the encoding type BarcodeEncoding.Code128
.SaveAsPng()
method.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#