Barcodes & QRs in C# & VB.Net Applications
Reading and writing barcodes in C# and all other .NET languages is easy using the Iron Barcode software library from Iron Software.
Install IronBarcode
The first step in the journey will be installing Iron Barcode, which may be achieved by using our NuGet package or by downloading the DLL from this website. of the Iron Barcode classes can be found in the IronBarcode
namespace.
The easiest way to install IronBarcode is using the NuGet Package Manager for Visual-Studio: The package name is “Barcode”.
PM > Install-Package Barcode
https://www.nuget.org/packages/Barcode/
Reading a Barcode or QR Code
Reading a barcode with Iron Barcode takes simply takes a few lines of code.
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg");
if(Result != null){
Console.WriteLine(Result.Text)
}
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg");
if(Result != null){
Console.WriteLine(Result.Text)
}
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg")
If Result IsNot Nothing Then
Console.WriteLine(Result.Text)
End If
To improve speed and accuracy of reading barcodes, we can specify a specific barcode format (or formats) so that Iron Barcode doesn't have to try for every single type of barcode known. We may also pick a specific crop region or area of an image to look in, if that is known.
ReadArea = new System.Drawing.Rectangle(100,200,300,400);
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg", ReadArea, BarcodeEncoding.QRCode | BarcodeEncoding.Code128);
if(Result != null){
Console.WriteLine(Result.Text)
}
ReadArea = new System.Drawing.Rectangle(100,200,300,400);
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg", ReadArea, BarcodeEncoding.QRCode | BarcodeEncoding.Code128);
if(Result != null){
Console.WriteLine(Result.Text)
}
ReadArea = New System.Drawing.Rectangle(100,200,300,400)
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("QuickStart.jpg", ReadArea, BarcodeEncoding.QRCode Or BarcodeEncoding.Code128)
If Result IsNot Nothing Then
Console.WriteLine(Result.Text)
End If
We may also choose to scan for multiple barcodes in a simple scan.
BarcodeResult[] Results = BarcodeReader.QuicklyReadAllBarcodes("MultipleBarcodes.png");
// Work with 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);
}
BarcodeResult[] Results = BarcodeReader.QuicklyReadAllBarcodes("MultipleBarcodes.png");
// Work with 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);
}
Dim Results() As BarcodeResult = BarcodeReader.QuicklyReadAllBarcodes("MultipleBarcodes.png")
' Work with 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
We may also use Iron Barcode to read barcodes from one or multiple pages of PDF documents or multipage TIFFs.
PagedBarcodeResult[] PagedResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (BarcodeResult Result in PagedResults)
{
int PageNumb = 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:
BarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
PagedBarcodeResult[] PagedResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (BarcodeResult Result in PagedResults)
{
int PageNumb = 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:
BarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
Dim PagedResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf")
' Work with the results
For Each Result As BarcodeResult In PagedResults
Dim PageNumb 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 BarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None)
Writing Barcodes
To create or write barcodes using Iron Barcode, we use the BarcodeWriter class. With BarcodeWriter, writing barcodes is extremely easy. We simply specify the barcode format and the value we wish to express, and it will create an image which may be exported as a System.Drawing.Image object or saved to a file.
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.SaveAsImage("MyBarCode.png");
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.SaveAsImage("MyBarCode.png");
Dim MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
MyBarCode.SaveAsImage("MyBarCode.png")
Styling Barcodes
The visual depiction of a barcode can be manipulated very easily using Iron Barcode. The output of every BarcodeWriter operation is a generated barcode object. This generated barcode object has a Fluent API, allowing the graphical settings of the barcode to be set in a single line of code, similar to Linq.
Popular features for styling barcodes include resizing barcodes, setting margins, changing background colors, changing barcode colors, and verifying that the output barcode is still readable.
In a similar fashion, we may also add annotations such as text to a barcode in any typeface we want, or place the value of the barcode very easily under or above the barcode.
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.SaveAsImage("MyBarCode.png");
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.SaveAsImage("MyBarCode.png");
Dim MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:")
MyBarCode.AddBarcodeValueTextBelowBarcode()
MyBarCode.SaveAsImage("MyBarCode.png")
Exporting Barcodes as HTML
The generated barcode object also has a convenient feature to export 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.
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.SetMargins(100);
MyBarCode.ChangeBarCodeColor(Color.Purple);
MyBarCode.SaveAsPng("MyBarCode.png"); // All major image formats supported as well as PDF and HTML
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:");
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.SetMargins(100);
MyBarCode.ChangeBarCodeColor(Color.Purple);
MyBarCode.SaveAsPng("MyBarCode.png"); // All major image formats supported as well as PDF and HTML
Dim MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
MyBarCode.AddAnnotationTextAboveBarcode("Product URL:")
MyBarCode.AddBarcodeValueTextBelowBarcode()
MyBarCode.SetMargins(100)
MyBarCode.ChangeBarCodeColor(Color.Purple)
MyBarCode.SaveAsPng("MyBarCode.png") ' All major image formats supported as well as PDF and HTML
Generating QR Codes
When generating QR codes using Iron Barcode, we may opt to use the QR Code Writer class instead of the BarcodeWriter class. This class gives some new and interesting features for QR code writing. It allows us to set the QR error correction level, allowing you to decide on a balance between the size of your QR code and its ease of readability.
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
Iron Barcode also allows for QR codes to be stylized, such as having a logo graphic placed and snapped to grid in the exact center of the image. It may also be colorized so that it matches a certain brand or graphic identity.
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "2 Create a QR Code\\visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "2 Create a QR Code\\visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Dim MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "2 Create a QR Code\visual-studio-logo.png", 500)
MyQRWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
Supported Barcode Formats
Iron Barcode supports a large number of commonly used barcode formats:
- QR codes, including decorated and branded QR codes with logos and colors
- Multi-format barcodes, such as Aztec, Data Matrix, CODE 93, and CODE 128
- RSS Expanded Databar, UPS MaxiCode, and USPS IMb OneCode barcodes
- Stacked linear barcodes such as RSS-14 and PDF-417
- Conventional numerical barcode formats such as UPCA, UPCE, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.SaveAsImage("MyBarCode.png");
MyBarCode.SaveAsGif("MyBarCode.gif");
MyBarCode.SaveAsHtmlFile("MyBarCode.html");
MyBarCode.SaveAsJpeg("MyBarCode.jpg");
MyBarCode.SaveAsPdf("MyBarCode.Pdf");
MyBarCode.SaveAsPng("MyBarCode.png");
MyBarCode.SaveAsTiff("MyBarCode.tiff");
MyBarCode.SaveAsWindowsBitmap("MyBarCode.bmp");
Image MyBarCodeImage = MyBarCode.Image;
Bitmap MyBarCodeBitmap = MyBarCode.ToBitmap();
string DataURL = MyBarCode.ToDataUrl();
string ImgTagForHTML = MyBarCode.ToHtmlTag();
byte[] PngBytes = MyBarCode.ToPngBinaryData();
// Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
using (System.IO.Stream PdfStream = MyBarCode.ToPdfStream()){
// Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
}
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
var MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
MyBarCode.SaveAsImage("MyBarCode.png");
MyBarCode.SaveAsGif("MyBarCode.gif");
MyBarCode.SaveAsHtmlFile("MyBarCode.html");
MyBarCode.SaveAsJpeg("MyBarCode.jpg");
MyBarCode.SaveAsPdf("MyBarCode.Pdf");
MyBarCode.SaveAsPng("MyBarCode.png");
MyBarCode.SaveAsTiff("MyBarCode.tiff");
MyBarCode.SaveAsWindowsBitmap("MyBarCode.bmp");
Image MyBarCodeImage = MyBarCode.Image;
Bitmap MyBarCodeBitmap = MyBarCode.ToBitmap();
string DataURL = MyBarCode.ToDataUrl();
string ImgTagForHTML = MyBarCode.ToHtmlTag();
byte[] PngBytes = MyBarCode.ToPngBinaryData();
// Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
using (System.IO.Stream PdfStream = MyBarCode.ToPdfStream()){
// Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
}
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
Dim MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
MyBarCode.SaveAsImage("MyBarCode.png")
MyBarCode.SaveAsGif("MyBarCode.gif")
MyBarCode.SaveAsHtmlFile("MyBarCode.html")
MyBarCode.SaveAsJpeg("MyBarCode.jpg")
MyBarCode.SaveAsPdf("MyBarCode.Pdf")
MyBarCode.SaveAsPng("MyBarCode.png")
MyBarCode.SaveAsTiff("MyBarCode.tiff")
MyBarCode.SaveAsWindowsBitmap("MyBarCode.bmp")
Dim MyBarCodeImage As Image = MyBarCode.Image
Dim MyBarCodeBitmap As Bitmap = MyBarCode.ToBitmap()
Dim DataURL As String = MyBarCode.ToDataUrl()
Dim ImgTagForHTML As String = MyBarCode.ToHtmlTag()
Dim PngBytes() As Byte = MyBarCode.ToPngBinaryData()
' Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
Using PdfStream As System.IO.Stream = MyBarCode.ToPdfStream()
' Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
End Using
MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50)
Why Choose Iron Barcode?
Iron Barcode features a friendly API for developers to read and write barcodes for .NET, which optimizes for accuracy and a low error rate 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, Iron Barcode will instruct the developer of a more appropriate barcode format which they might use.
Iron Barcode shines in the area of reading barcodes when the barcode has been scanned or has been read from a photographic image. In other words, the image is not graphically perfect and is not just simply a machine-generated screenshot.
Iron Barcode includes automatic rotation and perspective correction, correction for digital noise, and can automatically detect the type of barcode which is encoded in an image.
Moving Forward
To get more out of Iron Barcode, we encourage you to read the tutorials within this documentation section, to visit us on GitHub, and to read the .NET API Reference in an MSDN format.