如何從流中讀取條碼C#

How to read Barcodes from Streams

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

.NET Framework 中的 MemoryStream 類提供了一種方式來讀寫存儲於內存中的流。 它是一種流類型,可以用於操作未存儲在物理文件而是存儲在內存中的數據。

除了從圖像文件或 PDF 文件中讀取條碼之外,IronBarcode 在從流中讀取條碼方面也表現出色。 作為應用中的一個強大 API,IronBarcode 能夠接受 PDF 文檔或圖像流作為輸入,並輸出流中條碼的讀取結果。 現在讓我們看看如何實現這一點。

快速入門:直接從圖像流讀取條碼

使用 IronBarcode 只需兩行代碼即可從任何圖像流中讀取條碼—無需先寫入磁盤。此簡單示例展示了在 .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
class="hsg-featured-snippet">

最小工作流(5步)

  1. 從圖像流中讀取條碼
  2. 從 PDF 文檔流中讀取條碼

從圖像流中讀取條碼

在本節中,我們將向您展示如何使用 IronBarcode 來讀取圖像流,以及存儲在 List<MemoryStream> 中的多個圖像流。 下面是更正後的代碼塊,附有註釋以幫助您理解過程:

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

從上面的代碼片段中可以看出,IronBarcode 可以接受單個或 MemoryStream 對象列表進入 BarcodeReader.Read() 方法中並將流對象作為輸入進行讀取。 該示例展示了如何將圖像文件轉換為 MemoryStream 對象並直接讀取流中的條碼。

從 PDF 文件流中讀取條碼

在本節中,我們將向您展示如何使用 IronBarcode 讀取作為 MemoryStream 對象的 PDF 文檔文件或 PDF 文檔的 MemoryStream 列表。 以下是改進後的代碼塊:

: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

如上面的代碼片段所示,從 PDF 文檔中讀取條碼作為 MemoryStream 對象與從圖像中讀取條碼非常相似。 主要區別在於所使用的方法:BarcodeReader.ReadPdf() 特別適用於 PDF 文檔。 在示例中,我們使用 IronPDF 庫作為幫助來將 PDF 文檔轉換為 MemoryStream 對象。 如果要閱讀多個 PDF 文檔,建議將所有 PDF 文檔合併為一個 PDF 文檔流並將其提供給 BarcodeReader.ReadPdf() 方法。 隨時進行嘗試並讓該庫滿足您的具體需求!

常見問題解答

如何在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?

在C#中將圖像文件轉換為MemoryStream,通過使用File.ReadAllBytes()讀取圖像位元組並將位元組數組傳遞給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 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布