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 BarCode

You can find more information about the NuGet package on the NuGet website.

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
VB   C#

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—everthing you need in one step! This method supports a wide range of image formats, including images like JPEG, PNG, and BMP, as well as PDFs and multi-frame formats such as GIF and TIFF. For those who need even more speed, optimized memory usage, or enhanced reading accuracy, customizable configuration options are available to fine-tune performance to your specific needs.

For one, to improve reading speed, you could add a BarcodeReaderOptions object with the Speed setting set. By default, it is set at Balanced, but there is also a Faster option which skips certain barcode reading checks such as checking for inverted colors or barcode rotation.

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

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    Speed = ReadingSpeed.Faster
};

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

Private myOptionsExample As New BarcodeReaderOptions() With {.Speed = ReadingSpeed.Faster}

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

You could also set the ScanMode to OnlyBasicScan to skip having our barcode detection algorithm scan the document.

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

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ScanMode = BarcodeScanMode.OnlyBasicScan
};

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

Private myOptionsExample As New BarcodeReaderOptions() With {.ScanMode = BarcodeScanMode.OnlyBasicScan}

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

Other configurations to try are specifying barcode formats to scan, so that IronBarcode does not have to scan for all barcode types, and cropping specific regions of the image, so IronBarcode has less image data to process. There's also an option for expecting multiple barcodes, so IronBarcode can skip certain processes for detecting and decoding multiple barcodes if only one is expected.

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.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
VB   C#

Writing Barcodes

To write barcodes using IronBarcode, we use the BarcodeWriter class. With BarcodeWriter, writing barcodes is another simple one line of code: just enter the input data and the barcode format, and you're good to go! You can then save the barcode image data as memory or as a file.

: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")
VB   C#

Styling Barcodes

IronBarcode offers several options to manipulate the visual depiction of a barcode. Supported features for styling barcodes include resizing, setting margins in the output image, changing foreground and background colors, and adding annotations.

: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")
VB   C#

Exporting Barcodes as HTML

There is also the convenient feature of exporting HTML from a generated barcode. It can export a barcode as a standalone HTML document with no external assets, as a standalone HTML tag, or as a data URI.

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

myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarcode

myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsHtmlFile("myBarcode.html")
VB   C#

Generating QR Codes

When generating QR codes using IronBarcode, we may opt to use the QRCodeWriter class instead. This class provides some more customization configurations exclusive to QR codes, such as setting the error correction level and adding an image in the center of the QR.

: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")
VB   C#

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, will automatically validate and correct 'checksums' on UPCA and UPCE barcodes. It will also 'zero-pad' numbers which are too short for a specific numeric format. If your data is inappropriate for the specified data format, IronBarcode will instruct the developer of a more appropriate barcode format which they might use.

IronBarcode shines both in the areas of reading barcodes from documents and reading barcodes from images taken in the real world. The library includes a number of image pre-processing techniques to maximize the probability of barcodes being read, such as automatic rotation and image denoising.

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.