IronBarcode 如何使用 .NET 從串流讀取 BarCode 如何使用 C# 從串流中讀取條碼 Hairil Hasyimi Bin Omar 更新:6月 10, 2025 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English MemoryStream 是 .NET Framework 中的一個類,它提供了一種從儲存在記憶體中的流讀取和寫入資料的方法。 它是一種可用於操作未儲存在實體檔案中,而是儲存在記憶體中的資料的流類型。 除了從圖像檔案或 PDF 檔案中讀取條碼外,IronBarcode 在從資料流中讀取條碼方面也表現出色。 作為應用程式中一個強大的 API,IronBarcode 能夠接受 PDF 文件或圖像流作為輸入,並輸出流中條碼的讀取結果。 現在讓我們來看看如何才能實現這一點。 快速入門:直接從圖像流讀取條碼 使用 IronBarcode,只需兩行程式碼即可從任何圖像流中讀取條碼——無需先寫入磁碟。這個簡單的範例展示了在 .NET 中入門基於流的條碼讀取是多麼容易。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronBarcode PM > Install-Package BarCode 複製並運行這段程式碼。 var result = IronBarCode.BarcodeReader.Read(myImageStream); Console.WriteLine(result[0].Text); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronBarcode,免費試用! 免費試用30天 最小工作流程(5 個步驟) 從影像流中讀取條碼 從 PDF 文件流程中讀取條碼 從影像流中讀取條碼 在本節中,我們將向您展示如何使用 IronBarcode 讀取影像流,以及如何讀取儲存在List<MemoryStream>中的多個影像流。 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 將 PDF 文件檔案讀取為MemoryStream物件或 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物件。 如果您希望 IronBarcode 讀取多個 PDF 文檔,建議將所有 PDF 文檔合併到一個 PDF 文檔流中,並將其提供給BarcodeReader.ReadPdf()方法。 您可以隨意嘗試,並根據您的特定需求調整庫的使用方式! 常見問題解答 如何用 C# 從影像串流讀取 BarCode? 您可以使用 IronBarcode 在 C# 中從影像流中讀取 BarCode。將影像檔案轉換為 MemoryStream 物件,然後運用 BarcodeReader.Read() 方法來從這些串流中解碼條碼。 如何使用 .NET 從 PDF 文件流中讀取 BarCode? 要在 .NET 中從 PDF 文件流中讀取條碼,請使用 IronBarcode 的 BarcodeReader.ReadPdf() 方法。首先,將您的 PDF 文件轉換成 MemoryStream 物件,可能的話使用 IronPDF,然後將它們傳給該方法。 我可以從單一 PDF 串流讀取多個 BarCode 嗎? 是的,IronBarcode 的 BarcodeReader.ReadPdf() 方法可以處理單個 PDF 流並返回多個 BarcodeResult 物件,每個物件代表在 PDF 中找到的條碼。 在 .NET 中使用 MemoryStream 進行 BarCode 讀取有什麼優勢? 使用 MemoryStream 可以高效地在記憶體中操作資料,因此非常適合想要讀取條碼而不依賴實體檔案儲存的場景。 如果有多個 PDF 文件需要處理 BarCode,該怎麼辦? 如果您有多個 PDF 文件,請考慮將它們合併為單一 PDF 串流。這可以使用 BarcodeReader.ReadPdf() 方法有效地讀取所有條碼。 是否可以在單一應用程式中同時讀取影像和 PDF 串流中的 BarCode? 是的,IronBarcode 支持从图像和 PDF 数据流中读取条码。您可以在同一個應用程式中使用 BarcodeReader.Read() 來讀取圖片,並使用 BarcodeReader.ReadPdf() 來讀取 PDF。 如何在 C# 中將影像檔案轉換為 MemoryStream? 在 C# 中透過使用 File.ReadAllBytes() 讀取影像位元組,並將位元組陣列傳給 MemoryStream 建構器將影像檔案轉換為 MemoryStream 。 我需要額外的函式庫才能將 PDF 檔案轉換為 MemoryStreams 嗎? IronPDF 可用於促進 PDF 檔案轉換為 MemoryStream 物件,而 IronBarcode 則可直接處理 PDF 串流以進行條碼讀取。 從影像和 PDF 串流讀取 BarCode 有何差異? 主要的差異在於使用的方法:BarcodeReader.Read() 是用於影像串流,而 BarcodeReader.ReadPdf() 則是專門用於 PDF 文件串流。 是否有用 C# 從串流讀取 BarCode 的範例程式碼? 是的,文章提供了示例 C# 程式碼,示範如何使用 IronBarcode 從影像和 PDF 串流讀取條碼,並強調轉換為 MemoryStream 物件的過程。 Hairil Hasyimi Bin Omar 立即與工程團隊聊天 軟體工程師 和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。 準備好開始了嗎? Nuget 下載 1,979,979 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:1,979,979 檢視授權