How to read Barcodes from Streams

by Hairil Hasyimi Bin Omar



MemoryStream is a class in the .NET Framework that provides a way to read from and write to a stream that is stored in memory. It is a type of stream that can be used to manipulate data that is not stored in a physical file, but rather in memory.

Apart from reading barcodes from image files or PDF files, IronBarcode also excels in reading barcodes from streams. Being a great API in an application, IronBarcode is able to accept PDF document or image stream as an input and output the read result of the barcodes inside the stream. Now let us see how we can achieve this.

Read barcodes from Image stream

In this section, we will show you on how to use IronBarcode to read an image stream, as well as multiple image streams stored in a List<>

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-streams-1.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.IO;

List<MemoryStream> list = new List<MemoryStream>();
list.Add(AnyBitmap.FromFile("image1.jpg").ToStream());
list.Add(AnyBitmap.FromFile("image2.jpg").ToStream());
list.Add(AnyBitmap.FromFile("image3.png").ToStream());

var myBarcode = BarcodeReader.Read(list);

foreach (var barcode in myBarcode)
{
    Console.WriteLine(barcode.ToString());
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.IO

Private list As New List(Of MemoryStream)()
list.Add(AnyBitmap.FromFile("image1.jpg").ToStream())
list.Add(AnyBitmap.FromFile("image2.jpg").ToStream())
list.Add(AnyBitmap.FromFile("image3.png").ToStream())

Dim myBarcode = BarcodeReader.Read(list)

For Each barcode In myBarcode
	Console.WriteLine(barcode.ToString())
Next barcode
VB   C#

From the code snippet above, we can see that IronBarcode can accept object, as well as list of objects of MemoryStream type into the BarcodeReader.Read() method and read the stream objects. In the code snippet above, we also introduced our free, open source library, IronDrawing that can be used to convert images to MemoryStream objects. However, in case you already have an image or list of images asobjects, you can directly use it as argument in BarcodeReader.Read() to read the barcodes present in the stream.

Read barcodes from PDF document stream

In this section, we will show you on how to use IronBarcode to read PDF document file as a MemoryStream object or a MemoryStream list of PDF documents.

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-streams-2.cs
using IronBarCode;
using IronPdf;
using System;
using System.IO;

MemoryStream document = PdfDocument.FromFile(@"file_path.pdf").Stream;

var myBarcode = BarcodeReader.ReadPdf(document);

foreach (var value in myBarcode)
{
    Console.WriteLine(value.ToString());
}
Imports IronBarCode
Imports IronPdf
Imports System
Imports System.IO

Private document As MemoryStream = PdfDocument.FromFile("file_path.pdf").Stream

Private myBarcode = BarcodeReader.ReadPdf(document)

For Each value In myBarcode
	Console.WriteLine(value.ToString())
Next value
VB   C#

As seen from the code snippet above, there is not much difference in how to read barcodes from PDF document as a MemoryStream object with reading barcode as a MemoryStream object. The only difference is the reading method used to read barcodes from PDF documents, which is BarcodeReade.ReadPdf() that accepts PDF document as a single MemoryStream object. In the code snippet above also, we used IronPDF as helper to convert PDF document to MemoryStream object. If there are multiple PDF documents as stream that you want IronBarcode to read, it is recommended to merge all the PDF documents into one PDF document stream and feed it to BarcodeReader.ReadPdf() method. So, feel free to try it and manipulate the library the way you want!!

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.