How to read Barcodes from Streams

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 how to use IronBarcode to read an image stream, as well as multiple image streams stored in a List<MemoryStream>. Below is the corrected block of code with comments to help you understand the process:

using IronBarCode;
using System;
using System.Collections.Generic;
using System.IO;

class BarcodeFromImageStream
{
    static void Main(string[] args)
    {
        // Create a list of MemoryStreams to store image streams
        List<MemoryStream> imageStreams = new List<MemoryStream>
        {
            // Example of adding an existing MemoryStream object to the list
            new MemoryStream(File.ReadAllBytes("example1.png")),
            new MemoryStream(File.ReadAllBytes("example2.png"))
        };

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

class BarcodeFromImageStream
{
    static void Main(string[] args)
    {
        // Create a list of MemoryStreams to store image streams
        List<MemoryStream> imageStreams = new List<MemoryStream>
        {
            // Example of adding an existing MemoryStream object to the list
            new MemoryStream(File.ReadAllBytes("example1.png")),
            new MemoryStream(File.ReadAllBytes("example2.png"))
        };

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 System
Imports System.Collections.Generic
Imports System.IO

Friend Class BarcodeFromImageStream
	Shared Sub Main(ByVal args() As String)
		' Create a list of MemoryStreams to store image streams
		Dim imageStreams As New List(Of MemoryStream) From {
			New MemoryStream(File.ReadAllBytes("example1.png")),
			New MemoryStream(File.ReadAllBytes("example2.png"))
		}

Dim IronBarCode As using
Using IronSoftware.Drawing
	Dim System As using
	Using System.Collections.Generic
		Using System.IO
			
			Dim 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
		End Using
	End Using
End Using
$vbLabelText   $csharpLabel

From the code snippet above, IronBarcode can accept an object or a list of MemoryStream objects into the BarcodeReader.Read() method and read the stream objects as input. The example shows how to convert an image file into a MemoryStream object and read the barcodes present in the stream directly.

Read barcodes from PDF document stream

In this section, we will show you how to use IronBarcode to read a PDF document file as a MemoryStream object or a MemoryStream list of PDF documents. Here is the improved block of code:

: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
$vbLabelText   $csharpLabel

As seen in the code snippet above, reading barcodes from a PDF document as a MemoryStream object is quite similar to reading barcodes from images. The primary difference is the method used: BarcodeReader.ReadPdf() is specifically used for PDF documents. In the example, we use the IronPDF library as a helper to convert a PDF document to a MemoryStream object. If there are multiple PDF documents that you want IronBarcode to read, it is recommended to merge all the PDF documents into one PDF document stream and feed it to the BarcodeReader.ReadPdf() method. Feel free to experiment and make the library work for your specific needs!

Frequently Asked Questions

How can I read barcodes from an image stream using a .NET library?

To read barcodes from an image stream using IronBarcode, you can create a list of MemoryStreams containing your image data and use the BarcodeReader.Read() method to decode the barcodes from each stream.

What is the purpose of MemoryStream in .NET?

MemoryStream is a class in the .NET Framework that allows you to read from and write to a stream stored in memory, rather than a physical file. It's useful for manipulating in-memory data.

Can a .NET library read barcodes from PDF streams?

Yes, IronBarcode can read barcodes from a PDF document stream. You can convert the PDF file to a MemoryStream and use the BarcodeReader.ReadPdf() method to decode the barcodes.

What is the difference between reading barcodes from image streams and PDF streams?

The primary difference is the method used: BarcodeReader.Read() is used for image streams, while BarcodeReader.ReadPdf() is used for PDF document streams.

How do I convert an image file to a MemoryStream?

You can convert an image file to a MemoryStream by reading the file bytes using File.ReadAllBytes() and passing the byte array to the MemoryStream constructor.

Is it possible to read multiple barcodes from a single PDF stream?

Yes, it is possible. The BarcodeReader.ReadPdf() method can return an array of BarcodeResult objects, one for each barcode found in the PDF stream.

Can I use a .NET library with a list of MemoryStream objects?

Yes, IronBarcode can accept a list of MemoryStream objects, allowing you to read barcodes from multiple image or PDF streams.

What should I do if I have multiple PDF documents to read?

If you have multiple PDF documents, it is recommended to merge them into one PDF document stream before using the BarcodeReader.ReadPdf() method.

Do I need a PDF library to read PDF streams with a .NET barcode library?

While IronPDF can assist in converting PDF documents to MemoryStreams, you can read PDF streams directly with IronBarcode using the BarcodeReader.ReadPdf() method.

Is there a sample code for reading barcodes from image and PDF streams?

Yes, the tutorial provides sample code that demonstrates how to read barcodes from both image and PDF streams using IronBarcode.

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.