How to Read Barcodes from Streams with C#
IronBarcode reads barcodes directly from MemoryStream objects in .NET applications, eliminating the need to save streams to disk first. This enables efficient barcode processing from both image and PDF document streams stored in memory.
MemoryStream is a .NET Framework class that reads from and writes to memory-stored streams. It manipulates data without physical files, storing it in memory instead. This approach works well for web applications, APIs, or scenarios where you receive barcode data as byte arrays or need to process images without creating temporary files.
Beyond reading barcodes from image files or PDF files, IronBarcode also reads barcodes from streams. The library accepts PDF document or image streams as input and outputs the barcode reading results. This capability suits processing data from databases, web uploads, or memory-cached content.
Quickstart: Read Barcode Directly from Image Stream
Use two lines of code with IronBarcode to read barcodes from any image stream—no disk writing required. This example demonstrates stream-based barcode reading in .NET.
Get started making PDFs with NuGet now:
Install IronBarcode with NuGet Package Manager
Copy and run this code snippet.
var result = IronBarCode.BarcodeReader.Read(myImageStream); Console.WriteLine(result[0].Text);Deploy to test on your live environment
Minimal Workflow (5 steps)
- Read barcodes from Image stream
- Read Barcodes from PDF document stream
How Do I Read Barcodes from Image Streams?
Why Use Memory Streams Instead of Files?
Memory streams offer several advantages over file-based operations. They eliminate disk I/O overhead, making applications faster. They increase security since sensitive barcode data never touches the disk. They also suit cloud environments where disk access is limited or expensive. Combined with IronBarcode's async and multithreading capabilities, you can process multiple streams concurrently for maximum performance.
This section shows how to use IronBarcode to read an image stream and multiple image streams stored in a List<MemoryStream>. The following code includes comments to explain the process:
What Types of Image Formats Are Supported?
IronBarcode supports multiple image formats when reading from streams: JPEG, PNG, GIF, TIFF, BMP, and SVG. This flexibility allows processing barcode images regardless of source format. For optimal results, use the barcode reader settings to fine-tune the reading 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.csusing 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 UsingHow Do I Handle Multiple Image Streams?
The code above shows IronBarcode accepting a single MemoryStream object or a list of MemoryStream objects in the BarcodeReader.Read() method. The example converts image files into MemoryStream objects and reads barcodes directly from the streams.
When processing multiple streams, IronBarcode uses its reading speed options efficiently. Adjust reading speed based on your needs—whether requiring maximum accuracy or fastest processing time. For reading multiple barcodes from a single stream, IronBarcode automatically detects and returns all found barcodes.
For enhanced accuracy with imperfect or low-quality images in streams, apply image correction filters before processing. This improves barcode recognition rates significantly.
How Do I Read Barcodes from PDF Document Streams?
Why Use ReadPdf() Instead of Read()?
The ReadPdf() method optimizes specifically for PDF documents, offering advantages over the generic Read() method. It handles PDF-specific features like multi-page documents, vector graphics, and embedded images efficiently. The method processes all PDF pages automatically and handles various PDF formats and compression types. For comprehensive features, see the reading barcodes tutorial.
This section demonstrates using IronBarcode to read PDF document files as MemoryStream objects or lists of PDF document streams. Here's the code:
What About Multiple PDF Documents?
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-streams-2.csusing 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 valueHow Do I Process Multiple PDF Streams Efficiently?
Reading barcodes from PDF documents as MemoryStream objects resembles reading from images. The key difference is the method: BarcodeReader.ReadPdf() specifically handles PDF documents. The example uses IronPDF to convert PDF documents to MemoryStream objects.
For multiple PDF documents, merge all PDFs into one document stream before feeding it to BarcodeReader.ReadPdf(). This approach processes more efficiently than handling each PDF separately and reduces memory overhead. Configure PDF-specific barcode reader settings to optimize reading for your documents.
For advanced scenarios, process specific pages or PDF regions. IronBarcode allows specifying page ranges and cropping regions within pages, targeting areas where barcodes appear. This improves processing speed for large documents.
When using streams in production, properly dispose of MemoryStream objects to prevent memory leaks. The using statement ensures automatic disposal:
using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
{
var results = BarcodeReader.ReadPdf(pdfStream);
// Process results
}using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
{
var results = BarcodeReader.ReadPdf(pdfStream);
// Process results
}Imports System.IO
Using pdfStream As New MemoryStream(pdfBytes)
Dim results = BarcodeReader.ReadPdf(pdfStream)
' Process results
End UsingExperiment and adapt the library for your specific needs. IronBarcode's flexible API allows extensive reading process customization, from adjusting detection sensitivity to specifying expected barcode formats.
Frequently Asked Questions
How do I read barcodes from streams without saving to disk first?
IronBarcode allows you to read barcodes directly from MemoryStream objects without saving to disk. Simply pass your stream to the BarcodeReader.Read() method. This approach eliminates disk I/O overhead and is ideal for processing images from web uploads, databases, or APIs.
What image formats does the barcode reader support when processing streams?
IronBarcode supports multiple image formats when reading from streams, including JPEG, PNG, GIF, TIFF, BMP, and SVG. This flexibility allows you to process barcode images from various sources without format conversion.
Can I process multiple barcode image streams concurrently?
Yes, IronBarcode supports async and multithreading capabilities, allowing you to process multiple barcode streams concurrently for maximum performance. This is particularly useful when handling bulk barcode reading operations from memory.
What are the advantages of using memory streams over file-based barcode reading?
Memory stream processing with IronBarcode offers faster performance by eliminating disk I/O overhead, enhanced security since sensitive data never touches disk storage, and better compatibility with cloud environments where disk access may be limited or expensive.
How can I read barcodes from PDF document streams?
IronBarcode can read barcodes from both image and PDF document streams. Simply pass your PDF MemoryStream to the BarcodeReader.Read() method, and it will extract and decode all barcodes found within the PDF pages.
Is it possible to fine-tune the barcode reading process when working with streams?
Yes, IronBarcode provides customizable barcode reader settings that allow you to optimize the reading process for your specific use case, whether processing single streams or multiple streams stored in collections.






