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, you will learn how to read barcodes in C# using the Iron Barcode library. First, open Visual Studio and navigate to the Package Manager Console to install the Iron Barcode library by typing:
Install-Package BarCode
Once installed, proceed to the Program.cs file to write the necessary code. Begin by adding the proper namespace:
using IronBarCode;
using IronBarCode;
Imports IronBarCode
Reading a barcode is simplified with the method BarcodeReader.QuicklyReadOneBarcode
, which takes the file name of the barcode image as a parameter. Here is an example showing how to use this method:
using System;
using IronBarCode;
namespace BarcodeReaderExample
{
class Program
{
static void Main(string[] args)
{
// File path to the barcode image
string barcodeImagePath = "barcode.png";
// Try to read a single barcode from the image
var barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath);
// Check if the barcode was successfully read
if (barcodeResult != null)
{
// Print the decoded barcode value to the console
Console.WriteLine($"Barcode Value: {barcodeResult.Text}");
}
else
{
// Inform the user that no barcode was found
Console.WriteLine("No barcode found in the image.");
}
}
}
}
using System;
using IronBarCode;
namespace BarcodeReaderExample
{
class Program
{
static void Main(string[] args)
{
// File path to the barcode image
string barcodeImagePath = "barcode.png";
// Try to read a single barcode from the image
var barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath);
// Check if the barcode was successfully read
if (barcodeResult != null)
{
// Print the decoded barcode value to the console
Console.WriteLine($"Barcode Value: {barcodeResult.Text}");
}
else
{
// Inform the user that no barcode was found
Console.WriteLine("No barcode found in the image.");
}
}
}
}
Imports System
Imports IronBarCode
Namespace BarcodeReaderExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' File path to the barcode image
Dim barcodeImagePath As String = "barcode.png"
' Try to read a single barcode from the image
Dim barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath)
' Check if the barcode was successfully read
If barcodeResult IsNot Nothing Then
' Print the decoded barcode value to the console
Console.WriteLine($"Barcode Value: {barcodeResult.Text}")
Else
' Inform the user that no barcode was found
Console.WriteLine("No barcode found in the image.")
End If
End Sub
End Class
End Namespace
Key Points:
barcode.png
image is placed in the output directory of your project, typically the bin
folder, to make sure it can be accessed when the program runs.QuicklyReadOneBarcode
method attempts to read a single barcode from the provided image file. It returns a BarcodeResult
object upon success, or null
if no barcode is found.Running the program will successfully read and display the barcode value. This straightforward approach enables efficient barcode reading using C#.
Further Reading: Read Barcodes in C#