How to Output Data Formats

Instead of simply reading the barcode and printing the values in the console, IronBarcode offers much more. It provides several output formats that pave the way for users to further process the read results. These formats include properties such as barcode image, barcode type, BinaryValue, coordinates, height, width, page number, barcode, page orientation, text, and value.

Users can manipulate these properties further within the program. Let's explore how to use these properties and the use cases where they can be helpful.

Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer


Output Formats and Use Cases

BarcodeResult stores various useful properties. These properties are listed below:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • Coordinates, Height & Width
  • PageNumber
  • Barcode and PageOrientation
  • Text & Value

Barcode Image

Once IronBarcode performs a reading process on an image, the barcodes found in the image will be stored in BarcodeResult as the BarcodeImage property of type AnyBitmap. The BarcodeImage property stores the found barcode images. Users can retrieve this object to further process the image or save it as a permanent copy. This provides efficient and ease of use by eliminating the need to write additional code to extract the barcode images from an image.

Let us look at the code snippet below that demonstrates a possible use case for this output format:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeImage.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;

// This code snippet reads barcodes from a PDF file and saves them as a multi-page TIFF image.

try
{
    // Read barcodes from a PDF file.
    BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

    // Create a list to store barcode images extracted from the PDF.
    List<AnyBitmap> barcodeList = new List<AnyBitmap>();

    // Iterate through each detected barcode result.
    foreach (BarcodeResult barcode in result.Results)
    {
        // Add the barcode image to the barcode list.
        barcodeList.Add(barcode.BarcodeImage);
    }

    // Create a multi-page TIFF image from the list of barcode images and save it as 'barcodeImages.tif'.
    AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif");
}
catch (Exception ex)
{
    // Log the exception if reading or saving fails.
    Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Collections.Generic

' This code snippet reads barcodes from a PDF file and saves them as a multi-page TIFF image.

Try
	' Read barcodes from a PDF file.
	Dim result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

	' Create a list to store barcode images extracted from the PDF.
	Dim barcodeList As New List(Of AnyBitmap)()

	' Iterate through each detected barcode result.
	For Each barcode As BarcodeResult In result.Results
		' Add the barcode image to the barcode list.
		barcodeList.Add(barcode.BarcodeImage)
	Next barcode

	' Create a multi-page TIFF image from the list of barcode images and save it as 'barcodeImages.tif'.
	AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif")
Catch ex As Exception
	' Log the exception if reading or saving fails.
	Console.WriteLine($"An error occurred: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

The code snippet above illustrates one of the use cases for this output format. Specifically, it's designed to create a multipage TIFF image from barcodes detected within a PDF document. First, we scan or detect barcodes in the sample PDF. Then, we create a list of AnyBitmap where we store the information from the BarcodeImage property. Finally, we utilize this list to generate a multi-page TIFF using the CreateMultiFrameTiff method.

Please note
BarcodeImage property from BarcodeResult only stores the images of barcodes found during the read, not the whole input image itself.

Barcode Type

This property helps users in determining what type of barcode is present in the input image or document. However, the limitation of this feature is that the barcode type inside the image must be those that are supported and can be read by IronBarcode. To know more about supported barcode types in IronBarcode, users can refer to this article.

The code snippet below demonstrates how users can retrieve the barcode values as well as the barcode type in an image by printing the values on console.

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeType.cs
using IronBarCode;
using System;

// Assuming the IronBarCode library is correctly referenced and installed in your project.
// This code snippet demonstrates how to read a barcode from a PNG file and output its details to the console.

// Read barcodes from the specified PNG file.
BarcodeResults results = BarcodeReader.Read("bc3.png");

// Iterate through each barcode found in the results.
foreach (BarcodeResult barcode in results)
{
    // Output the barcode value and its type to the console.
    Console.WriteLine("The barcode value is: " + barcode.Text + " and the barcode type is: " + barcode.BarcodeType);
}
Imports IronBarCode
Imports System

' Assuming the IronBarCode library is correctly referenced and installed in your project.
' This code snippet demonstrates how to read a barcode from a PNG file and output its details to the console.

' Read barcodes from the specified PNG file.
Private results As BarcodeResults = BarcodeReader.Read("bc3.png")

' Iterate through each barcode found in the results.
For Each barcode As BarcodeResult In results
	' Output the barcode value and its type to the console.
	Console.WriteLine("The barcode value is: " & barcode.Text & " and the barcode type is: " & barcode.BarcodeType)
Next barcode
$vbLabelText   $csharpLabel

From the code snippet above, we perform barcode reading by calling BarcodeReader.Read() method on the input image. This returns a BarcodeResults object that stores all the BarcodeResult from reading all barcodes available in the image. Next, we iterate through the BarcodeResults object to retrieve the BarcodeResult and get the barcode value and type printed to the console.

Binary Value

Using IronBarcode, users can also retrieve the byte array of the barcode value by accessing the BinaryValue property from the BarcodeResult object. This allows users to manipulate the barcode value further inside the program.

The code snippet below demonstrates one use case of retrieving the barcode value as binary data:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BinaryValue.cs
// Import the IronBarCode library
using IronBarCode;

// Read barcodes from a PNG file
BarcodeResult[] results = BarcodeReader.Read("multiple-barcodes.png");

// Initialize a counter for naming output files
int i = 1;

// Process each barcode result
foreach (BarcodeResult barcode in results)
{
    // Retrieve the binary value of the barcode
    var binaryValue = barcode.BinaryValue;

    // Specify the barcode encoding type
    var barcodeType = BarcodeEncoding.QRCode;

    // Create a QR code based on the binary value and specified encoding type
    GeneratedBarcode generatedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType);

    // Export the generated QR code as a PNG file with an incremented filename
    generatedBarcode.SaveAsPng($"qrFromBinary{i}.png");

    // Increment the counter
    i++;
}
' Import the IronBarCode library
Imports IronBarCode

' Read barcodes from a PNG file
Private results() As BarcodeResult = BarcodeReader.Read("multiple-barcodes.png")

' Initialize a counter for naming output files
Private i As Integer = 1

' Process each barcode result
For Each barcode As BarcodeResult In results
	' Retrieve the binary value of the barcode
	Dim binaryValue = barcode.BinaryValue

	' Specify the barcode encoding type
	Dim barcodeType = BarcodeEncoding.QRCode

	' Create a QR code based on the binary value and specified encoding type
	Dim generatedBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType)

	' Export the generated QR code as a PNG file with an incremented filename
	generatedBarcode.SaveAsPng($"qrFromBinary{i}.png")

	' Increment the counter
	i += 1
Next barcode
$vbLabelText   $csharpLabel

Observing the code snippet above, we've created a straightforward program that transforms multiple barcodes within an image into separate new binary-encoded files. Initially, we scan the barcodes in the sample PNG image. Once we've detected these barcodes, we iterate through them, access the BinaryValue property, and then use it to create new binary files.

Barcode Coordinates, Height & Width

Another property of the BarcodeResult object that users can access is the barcode's coordinates, including X1, Y1, and X2, Y2, as well as its Height and Width within an image file or document. These properties prove to be very useful when users need to retrieve information about the barcode's location and dimensions.

Let's use an illustration to highlight the barcode's location and dimensions.

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-height-width.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Linq;
using System.Drawing;

// Read barcodes from a PNG file
// The method will return multiple barcode results found in the image
BarcodeResults results = BarcodeReader.Read("multiple-barcodes.png");

// Load the original image into an AnyBitmap object
AnyBitmap bitmap = AnyBitmap.FromFile("multiple-barcodes.png");

// Iterate through each detected barcode result
foreach (BarcodeResult barcode in results)
{
    // Extract the corner points (vertices) of the barcode
    PointF[] barcodePoints = barcode.Points;

    // Calculate the top-left corner of the bounding rectangle of the barcode
    float x1 = barcodePoints.Select(b => b.X).Min();
    float y1 = barcodePoints.Select(b => b.Y).Min();

    // Create a rectangle from the top-left corner position with the width and height of the barcode
    Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width, (int)barcode.Height);

    // Redact the detected barcode area with a magenta color
    // The Redact method fills the specified rectangle area with the given color
    bitmap = bitmap.Redact(rectangle, Color.Magenta);
}

// Save the redacted image as a new PNG file
bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png);
Imports System
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Linq
Imports System.Drawing

' Read barcodes from a PNG file
' The method will return multiple barcode results found in the image
Private results As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")

' Load the original image into an AnyBitmap object
Private bitmap As AnyBitmap = AnyBitmap.FromFile("multiple-barcodes.png")

' Iterate through each detected barcode result
For Each barcode As BarcodeResult In results
	' Extract the corner points (vertices) of the barcode
	Dim barcodePoints() As PointF = barcode.Points

	' Calculate the top-left corner of the bounding rectangle of the barcode
	Dim x1 As Single = barcodePoints.Select(Function(b) b.X).Min()
	Dim y1 As Single = barcodePoints.Select(Function(b) b.Y).Min()

	' Create a rectangle from the top-left corner position with the width and height of the barcode
	Dim rectangle As New Rectangle(CInt(Math.Truncate(x1)), CInt(Math.Truncate(y1)), CInt(barcode.Width), CInt(barcode.Height))

	' Redact the detected barcode area with a magenta color
	' The Redact method fills the specified rectangle area with the given color
	bitmap = bitmap.Redact(rectangle, Color.Magenta)
Next barcode

' Save the redacted image as a new PNG file
bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png)
$vbLabelText   $csharpLabel
Sample input before redaction
Redacted image

The code snippet above is used to redact multiple barcodes found in an image file. To achieve this, we use a combination of two libraries, IronBarcode and IronDrawing. To get the BarcodeResult object and extract the properties from it, we first read the barcodes available in an image file using the BarcodeReader.Read() method. Concurrently, the input image file also needs to be converted into an AnyBitmap object to apply the redaction method to the image. Once we have the BarcodeResults object, we can apply a loop and iterate through it to get the X1, Y1, Width, and Height of every barcode available in the image and use them in the CropRectangle properties of the AnyBitmap.Redact() method.

Page Number

Users can also retrieve the page number at which the barcode was found. This is a helpful feature for users who seek to use a multipage document that contains multiple barcodes and need to know the location of the barcodes found in the document for further processing.

Let's look at the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-page-number.cs
using IronBarCode;
using System;

// Read barcode from a PDF file and process the results
// BarcodeReader.ReadPdf returns a BarcodeResults object which contains multiple BarcodeResult objects.
// Each BarcodeResult contains details about a single barcode found in the PDF.
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output barcode values and their corresponding page numbers to the console
// Iterating over each BarcodeResult in the result object
foreach (BarcodeResult barcode in result)
{
    // The 'Value' property of BarcodeResult holds the decoded value of the barcode.
    // The 'PageNumber' property indicates the page number where the barcode was found in the PDF.
    Console.WriteLine("The barcode value " + barcode.Value + " is found on page number " + barcode.PageNumber);
}
Imports IronBarCode
Imports System

' Read barcode from a PDF file and process the results
' BarcodeReader.ReadPdf returns a BarcodeResults object which contains multiple BarcodeResult objects.
' Each BarcodeResult contains details about a single barcode found in the PDF.
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Output barcode values and their corresponding page numbers to the console
' Iterating over each BarcodeResult in the result object
For Each barcode As BarcodeResult In result
	' The 'Value' property of BarcodeResult holds the decoded value of the barcode.
	' The 'PageNumber' property indicates the page number where the barcode was found in the PDF.
	Console.WriteLine("The barcode value " & barcode.Value & " is found on page number " & barcode.PageNumber)
Next barcode
$vbLabelText   $csharpLabel

The simple code snippet above demonstrates one use case where users need the program to return the value of barcodes found in a multipage PDF document and their respective page numbers. The code snippet above uses the BarcodeReader.ReadPdf() method to read the barcodes inside a multipage PDF document, which returns BarcodeResults object storing every BarcodeResult found in the document. We apply a loop and iterate through each item in the object to retrieve the value of the barcodes and the page number where the barcodes were found. Apart from this use case, users can also use this property to debug if all barcodes in a document were able to be read.

Please note
The value returned from this property is 1-Based, meaning the first page is always one and not zero

Barcode rotation and Page Orientation

Using IronBarcode, users can also retrieve information on the barcode orientation and page orientation where the barcode was found. To extract these two pieces of information, users can access the Rotation and PageOrientation properties from the BarcodeResult object. Rotation will return an integer that represents the angle of rotation of the barcode found.

Let's look at the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-orientation.cs
using IronBarCode;
using System;

// Read barcodes from a PDF file
BarcodeResults results = BarcodeReader.ReadPdf("test.pdf");

// Iterate through each barcode result found in the PDF
foreach (BarcodeResult barcode in results.Results)
{
    // Output the value (information) of the barcode to the console
    Console.WriteLine("Barcode Value: " + barcode.Value);

    // Output the page number where the barcode was found
    Console.WriteLine("Page Number: " + barcode.PageNumber);
    
    // Output the page orientation where the barcode was found
    Console.WriteLine("Page Orientation: " + barcode.PageOrientation);

    // Output the rotation angle of the barcode
    Console.WriteLine("Rotation: " + barcode.Rotation);
}
Imports IronBarCode
Imports System

' Read barcodes from a PDF file
Private results As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Iterate through each barcode result found in the PDF
For Each barcode As BarcodeResult In results.Results
	' Output the value (information) of the barcode to the console
	Console.WriteLine("Barcode Value: " & barcode.Value)

	' Output the page number where the barcode was found
	Console.WriteLine("Page Number: " & barcode.PageNumber)

	' Output the page orientation where the barcode was found
	Console.WriteLine("Page Orientation: " & barcode.PageOrientation)

	' Output the rotation angle of the barcode
	Console.WriteLine("Rotation: " & barcode.Rotation)
Next barcode
$vbLabelText   $csharpLabel

The simple code snippet above was run with the sample PDF input attached to prove that users can retrieve the page orientation and barcode rotation by getting the value of BarcodeResult.PageOrientation and BarcodeResult.Rotation, respectively. This feature is useful mainly for debugging purposes.

Please note
IronBarcode can only read barcodes with rotations of 0, 90, 180, and 270 degrees. IronBarcode will not return any value if the barcode has a rotation value other than those mentioned. PageOrientation will return a PageOrientation object, which consists of Portrait or Landscape.

Text and Value

Of course, the main property that users will want to retrieve when using IronBarcode is its value and text. These two properties are often used interchangeably and will return the same value. Apart from that, users can also use the BarcodeResult.ToString() method to achieve the same result. The code snippet below demonstrates:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-text-value.cs
using IronBarCode;
using System;

// This code reads barcodes from a PDF file and outputs their values, text, and additional information to the console.
// Ensure that the IronBarCode library is properly referenced in your project.
// The specified PDF file should also exist in the expected directory or provide the full path to the file.

try
{
    // Read all barcodes from the specified PDF file.
    // Replace "barcodestamped3.pdf" with the path to your actual PDF file.
    BarcodeResults results = BarcodeReader.ReadPdf("barcodestamped3.pdf");

    // Iterate through each barcode result.
    foreach (BarcodeResult barcode in results)
    {
        // Output the decoded value of the barcode to the console.
        Console.WriteLine("Barcode Value: " + barcode.Value);

        // Output any additional text or descriptions of the barcode to the console. 
        // Often this could be the same as Value or include further information.
        Console.WriteLine("Barcode Text: " + barcode.Text);

        // Output the string representation of the barcode result to the console,
        // which may include value, location, and other meta-information.
        Console.WriteLine("Barcode Full Info: " + barcode.ToString());
    }
}
catch (Exception ex)
{
    // Catch and output any errors to the console.
    // Common errors might be related to file access, 
    // such as a missing file, incorrect file path, or permission issues.
    Console.WriteLine("Error reading barcode from PDF: " + ex.Message);
}
Imports IronBarCode
Imports System

' This code reads barcodes from a PDF file and outputs their values, text, and additional information to the console.
' Ensure that the IronBarCode library is properly referenced in your project.
' The specified PDF file should also exist in the expected directory or provide the full path to the file.

Try
	' Read all barcodes from the specified PDF file.
	' Replace "barcodestamped3.pdf" with the path to your actual PDF file.
	Dim results As BarcodeResults = BarcodeReader.ReadPdf("barcodestamped3.pdf")

	' Iterate through each barcode result.
	For Each barcode As BarcodeResult In results
		' Output the decoded value of the barcode to the console.
		Console.WriteLine("Barcode Value: " & barcode.Value)

		' Output any additional text or descriptions of the barcode to the console. 
		' Often this could be the same as Value or include further information.
		Console.WriteLine("Barcode Text: " & barcode.Text)

		' Output the string representation of the barcode result to the console,
		' which may include value, location, and other meta-information.
		Console.WriteLine("Barcode Full Info: " & barcode.ToString())
	Next barcode
Catch ex As Exception
	' Catch and output any errors to the console.
	' Common errors might be related to file access, 
	' such as a missing file, incorrect file path, or permission issues.
	Console.WriteLine("Error reading barcode from PDF: " & ex.Message)
End Try
$vbLabelText   $csharpLabel

From the code snippet above, users only need to use a few lines of code to read barcodes in an image using IronBarcode. After iterating through the BarcodeResults returned by the BarcodeReader.Read() method, we output to the console the result of getting the value and text properties, as well as calling the BarcodeResult.ToString() method to show that all these returned the same value.

In a nutshell, IronBarcode is a perfect API for users to perform multiple operations regarding barcodes, not limited to writing and decoding barcodes. With various output data formats supported, users can do so much more with the BarcodeResult object returned by IronBarcode.

Frequently Asked Questions

What output formats does IronBarcode offer for barcodes in .NET C#?

IronBarcode provides several output formats including barcode image, barcode type, BinaryValue, coordinates, height, width, page number, barcode, page orientation, text, and value.

How can I create a multipage TIFF image from detected barcodes using IronBarcode?

You can use the AnyBitmap class to create a multipage TIFF image from detected barcodes by first reading the barcodes from a PDF and then adding the BarcodeImage properties to a list, which can be used to generate the TIFF.

What is the purpose of the BarcodeType property in IronBarcode?

The BarcodeType property helps determine the type of barcode present in the input image or document. Users can print the barcode value and type to the console using this property.

How can I convert barcode values to binary data using IronBarcode?

To convert barcode values to binary data, you can read the barcodes from an image and access the BinaryValue property from the BarcodeResult object, which can then be saved as binary-encoded files.

How can I retrieve barcode coordinates, height, and width using IronBarcode?

You can retrieve barcode coordinates, height, and width by accessing the properties from the BarcodeResult object after reading barcodes from an image. This information can be used for tasks like redacting barcode images.

How do I find the page number a barcode is located on using IronBarcode?

The PageNumber property of the BarcodeResult object allows users to retrieve the page number where the barcode was found, which is particularly useful for multi-page documents.

Can IronBarcode determine the orientation of a barcode?

Yes, IronBarcode can determine barcode orientation and page orientation using the Rotation and PageOrientation properties of the BarcodeResult object.

What is the difference between the text and value properties in IronBarcode?

The text and value properties in IronBarcode are often used interchangeably and return the same value. The BarcodeResult.ToString() method can also be used to achieve the same result.

What are the limitations of barcode rotation detection in IronBarcode?

IronBarcode can only read barcodes with rotations of 0, 90, 180, and 270 degrees. It will not return any value for barcodes with other rotation values.

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.