How to Read Barcodes From Streams in C#

How to read Barcodes from Streams

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Read Barcode Directly from Image Stream

Use just two lines of code with IronBarcode to read barcodes from any image stream—no need to write to disk first. This quick example shows how simple it is to get started with stream-based barcode reading in .NET.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var result = IronBarCode.BarcodeReader.Read(myImageStream);
    Console.WriteLine(result[0].Text);
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

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!

常见问题解答

如何在C#中从图像流中读取条形码?

您可以使用IronBarcode在C#中从图像流中读取条形码。将图像文件转换为MemoryStream对象,然后使用BarcodeReader.Read()方法从这些流中解码条形码。

如何使用.NET从PDF文档流中读取条形码?

要在.NET中从PDF文档流中读取条形码,请使用IronBarcode的BarcodeReader.ReadPdf()方法。首先,将您的PDF文档转换为MemoryStream对象,可能使用IronPDF,然后将它们传递给该方法。

我可以从单个PDF流中读取多个条形码吗?

是的,IronBarcode的BarcodeReader.ReadPdf()方法可以处理单个PDF流并返回多个BarcodeResult对象,每个对象代表PDF中找到的一个条形码。

在.NET中使用MemoryStream进行条形码读取的优势是什么?

使用MemoryStream允许有效地在内存中操作数据,使其在您想要在不依赖物理文件存储的情况下读取条形码的情况下非常理想。

如果有多个PDF文档需要处理条形码,我应该怎么做?

如果您有多个PDF文档,请考虑将它们合并为一个PDF流。这可以使用BarcodeReader.ReadPdf()方法有效读取以提取所有条形码。

在单个应用程序中能否从图像和PDF流中读取条形码?

是的,IronBarcode支持从图像和PDF流中读取条形码。您可以在同一个应用程序中使用BarcodeReader.Read()读取图像并使用BarcodeReader.ReadPdf()读取PDF。

如何在C#中将图像文件转换为MemoryStream?

通过读取图像字节使用File.ReadAllBytes(),并将字节数组传递给MemoryStream构造函数来在C#中将图像文件转换为MemoryStream

我需要额外的库来将PDF文件转换为MemoryStreams吗?

虽然可以使用IronPDF来便捷地将PDF文件转换为MemoryStream对象,但IronBarcode可以直接处理PDF流以进行条形码读取。

从图像和PDF流中读取条形码有什么区别?

主要区别在于使用的方法:BarcodeReader.Read()用于图像流,而BarcodeReader.ReadPdf()专为PDF文档流设计。

有没有从流中读取条形码的示例代码在C#中?

是的,文章提供了示例C#代码,展示如何使用IronBarcode读取图像和PDF流中的条形码,强调将图像文件转换为MemoryStream对象的过程。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布