Barcodes & QRs in C# & VB.NET Applications

Reading and writing barcodes in C# and all other .NET languages is an easy process with our IronBarcode software library.

Install IronBarcode

The first step in the journey will be installing IronBarcode, which can be done by downloading from NuGet or by downloading the DLL.

To install the IronBarcode NuGet package, you could use the NuGet Package Manager for Visual Studio:

Install-Package BarCode

Alternatively, you could also install with the dotnet CLI:

dotnet add package IronBarCode

Reading a Barcode or QR Code

Reading a barcode only takes one line of code with IronBarcode.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
    foreach (BarcodeResult result in results)
    {
        Console.WriteLine(result.Text);
    }
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
	For Each result As BarcodeResult In results
		Console.WriteLine(result.Text)
	Next result
End If
$vbLabelText   $csharpLabel

With this one line of code, you have the ability to detect and scan all types of barcodes from the input document with exceptional performance—everything you need in one step! This method supports a wide range of image formats, such as JPEG, PNG, and BMP, as well as PDFs and multi-frame formats like GIF and TIFF. For enhanced performance, customizable configuration options are available.

To improve reading speed, you can create a BarcodeReaderOptions object with the Speed setting configured for better performance. The default is Balanced, but the Faster option is available to skip certain checks.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
	.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
$vbLabelText   $csharpLabel

You can also set the ScanMode to OnlyBasicScan to optimize the reading process.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");

// Loop through the results
foreach (BarcodeResult result in results)
{
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")

' Loop through the results
For Each result As BarcodeResult In results
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

Other configurations include specifying the barcode formats to scan for, which can help speed up processing by reducing unnecessary scans.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;

BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");

// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
    int pageNumber = result.PageNumber;
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}

// or from a multi-page  TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Multithreaded = false,
    RemoveFalsePositive = false,
    ImageFilters = null
});
Imports IronBarCode

Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")

' Loop through the results
For Each result As BarcodeResult In pagedResults
	Dim pageNumber As Integer = result.PageNumber
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result

' or from a multi-page  TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Detailed,
	.ExpectMultipleBarcodes = True,
	.ExpectBarcodeTypes = BarcodeEncoding.Code128,
	.Multithreaded = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
})
$vbLabelText   $csharpLabel

Writing Barcodes

To write barcodes using IronBarcode, we use the BarcodeWriter class.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
$vbLabelText   $csharpLabel

Styling Barcodes

IronBarcode offers several options to manipulate the visual depiction of a barcode.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);

// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)

' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
$vbLabelText   $csharpLabel

Exporting Barcodes as HTML

IronBarcode can export barcodes as HTML documents or as part of HTML content.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
$vbLabelText   $csharpLabel

Generating QR Codes

For QR codes, use the QRCodeWriter class which provides additional configuration for QR-specific features like error correction.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;

QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
$vbLabelText   $csharpLabel

Supported Barcode Formats

IronBarcode supports a wide variety of commonly used barcode formats for both reading and writing:

  • QR, Micro QR, and Rectangular Micro QR (rMQR) codes.
  • Other two-dimensional barcodes such as Aztec, Data Matrix, MaxiCode, and PDF417.
  • Stacked linear barcodes such as Databar.
  • Conventional one-dimensional barcode formats such as UPC-A, UPC-E, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey.

Why Choose IronBarcode?

IronBarcode offers a friendly, easy-to-use API for developers to read and write barcodes for .NET, which optimizes for accuracy, precision, and speed in real-world use cases.

The BarcodeWriter class, for example, automatically validates and corrects 'checksums' on UPCA and UPCE barcodes, and handles numeric format constraints. IronBarcode assists developers in choosing the most suitable barcode format for their data.

The library is robust, with image pre-processing techniques like automatic rotation and image denoising to maximize barcode detection success rates.

Moving Forward

To get the most out of IronBarcode, we encourage you to read the tutorials within this documentation section, and to visit us on GitHub.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.