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 do I read barcodes from an image stream in C#?

You can read barcodes from an image stream in C# by using IronBarcode. Convert the image files to MemoryStream objects and then utilize the BarcodeReader.Read() method to decode the barcodes from these streams.

How can I read barcodes from a PDF document stream using .NET?

To read barcodes from a PDF document stream in .NET, use IronBarcode's BarcodeReader.ReadPdf() method. First, convert your PDF documents to MemoryStream objects, possibly using IronPDF, and then pass them to the method.

Can I read multiple barcodes from a single PDF stream?

Yes, IronBarcode's BarcodeReader.ReadPdf() method can process a single PDF stream and return multiple BarcodeResult objects, each representing a barcode found in the PDF.

What is the advantage of using MemoryStream for barcode reading in .NET?

Using MemoryStream allows for efficient in-memory manipulation of data, making it ideal for scenarios where you want to read barcodes without relying on physical file storage.

What should I do if I have multiple PDF documents to process for barcodes?

If you have multiple PDF documents, consider merging them into a single PDF stream. This can be efficiently read using the BarcodeReader.ReadPdf() method to extract all barcodes.

Is it possible to read barcodes from both images and PDF streams in a single application?

Yes, IronBarcode supports reading barcodes from both image and PDF streams. You can use BarcodeReader.Read() for images and BarcodeReader.ReadPdf() for PDFs within the same application.

How can I convert an image file to a MemoryStream in C#?

Convert an image file to a MemoryStream in C# by reading the image bytes using File.ReadAllBytes() and passing the byte array to the MemoryStream constructor.

Do I need additional libraries to convert PDF files to MemoryStreams?

While IronPDF can be used to facilitate the conversion of PDF files to MemoryStream objects, IronBarcode can directly handle PDF streams for barcode reading.

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

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

Is there example code for reading barcodes from streams in C#?

Yes, the article provides sample C# code demonstrating how to read barcodes from both image and PDF streams using IronBarcode, highlighting the conversion process to MemoryStream objects.

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 ...Read More