IronBarcode 操作指南 .NET从流中读取条形码 How to Read Barcodes from Streams with C Hairil Hasyimi Bin Omar 已更新:2026年1月31日 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronBarcode直接从.NET应用程序中的 MemoryStream 对象读取条形码,无需先将数据流保存到磁盘。这使得它可以高效地处理存储在内存中的图像和 PDF 文档流中的条形码。 MemoryStream 是一个.NET Framework类,用于读取和写入内存存储的流。 它无需物理文件即可处理数据,而是将数据存储在内存中。 这种方法适用于网络应用程序、API 或以字节数组形式接收条形码数据或需要在不创建临时文件的情况下处理图像的场景。 除了从图像文件或PDF文件中读取条形码外,IronBarcode 还可以从数据流中读取条形码。 该库接受 PDF 文档或图像流作为输入,并输出条形码读取结果。 该功能适合处理来自数据库、网络上传或内存缓存内容的数据。 快速入门:直接从图像流读取条形码 使用 IronBarcode 只需两行代码即可从任何图像流中读取条形码--无需写入磁盘。 本示例演示了在 .NET 中基于流的 BarCode 读取。 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode PM > Install-Package BarCode 复制并运行这段代码。 var result = IronBarCode.BarcodeReader.Read(myImageStream); Console.WriteLine(result[0].Text); 部署到您的生产环境中进行测试 通过免费试用立即在您的项目中开始使用IronBarcode Free 30 Day Trial 最小工作流程(5 个步骤) 从图像流中读取条形码 从 PDF 文档流中读取条形码 如何从图像流中读取 BarCode? 为什么使用内存流而不是文件? 与基于文件的操作相比,内存流具有多项优势。 它们消除了磁盘 I/O 开销,使应用程序速度更快。 由于敏感的 BarCode 数据从未接触过磁盘,因此它们提高了安全性。 它们还适用于磁盘访问有限或昂贵的云环境。结合 IronBarcode 的 async 和多线程功能,您可以同时处理多个数据流,以获得最高性能。 本节展示了如何使用IronBarcode读取存储在 List<MemoryStream> 中的图像流和多个图像流。 以下代码包含注释,用于解释翻译过程: 支持哪些类型的图像格式? IronBarcode 从数据流中读取数据时支持多种图像格式:JPEG、PNG、GIF、TIFF、BMP 和 SVG。 这种灵活性允许处理条形码图像,而无需考虑源格式。 为获得最佳效果,请使用条码阅读器设置对阅读过程进行微调。 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()); } $vbLabelText $csharpLabel 如何处理多个图像流? 上面的代码显示IronBarcode在 MemoryStream 方法中接受单个 MemoryStream 对象或 BarcodeReader.Read() 对象列表。 该示例将图像文件转换为 MemoryStream 对象,并直接从流中读取条形码。 在处理多个数据流时,IronBarcode 会高效地使用其读取速度选项。 根据您的需求调整阅读速度--无论是要求最高的准确性还是最快的处理时间。对于从一个数据流中读取多个条码,IronBarcode会自动检测并返回所有找到的条码。 为提高流中不完美或低质量图像的准确性,请在处理前应用图像校正过滤器。 这样可以大大提高 BarCode 的识别率。 如何从 PDF 文档流中读取 BarCode? 为什么使用 ReadPdf() 而不是 Read()? ReadPdf() 方法专门针对 PDF 文档进行了优化,比通用的 Read() 方法具有优势。 它能有效处理 PDF 的特定功能,如多页文档、矢量图形和嵌入式图像。 该方法可自动处理所有 PDF 页面,并可处理各种 PDF 格式和压缩类型。有关全面的功能,请参阅读取 BarCode 教程。 本节演示如何使用IronBarcode将 PDF 文档文件读取为 MemoryStream 对象或 PDF 文档流列表。 以下是代码: 多个 PDF 文档怎么办? :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()); } $vbLabelText $csharpLabel 如何高效处理多个 PDF 流? 从 PDF 文档中读取条形码(MemoryStream 对象)类似于从图像中读取条形码。 主要区别在于方法:BarcodeReader.ReadPdf() 专门处理 PDF 文档。 该示例使用IronPDF将 PDF 文档转换为 MemoryStream 对象。 对于多个 PDF 文档,请将所有 PDF 合并到一个文档流中,然后再将其提供给 BarcodeReader.ReadPdf()。 与单独处理每个 PDF 相比,这种方法处理效率更高,还能减少内存开销。 配置PDF 专用条形码阅读器设置,以优化文档的读取。 对于高级场景,可处理特定页面或 PDF 区域。 IronBarcode 允许指定页面范围和页面内的裁剪区域,针对出现条形码的区域进行裁剪。 这样可以提高大型文档的处理速度。 在生产环境中使用流时,请正确释放 MemoryStream 对象,以防止内存泄漏。 using 语句确保自动处置: 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 } $vbLabelText $csharpLabel 请根据您的具体需求尝试和调整该库。 IronBarcode 灵活的 API 允许广泛的读取过程定制,从调整检测灵敏度到指定预期的条码格式。 常见问题解答 如何在不先保存到磁盘的情况下从数据流中读取 BarCode? IronBarcode 允许您直接从 MemoryStream 对象读取条形码,而无需保存到磁盘。只需将您的数据流传递给 BarcodeReader.Read() 方法即可。这种方法消除了磁盘 I/O 的开销,非常适合处理来自网络上传、数据库或 API 的图像。 条码阅读器在处理数据流时支持哪些图像格式? IronBarcode 从数据流中读取数据时支持多种图像格式,包括 JPEG、PNG、GIF、TIFF、BMP 和 SVG。这种灵活性使您无需进行格式转换即可处理各种来源的条码图像。 我可以同时处理多个 BarCode 图像流吗? 是的,IronBarcode 支持异步和多线程功能,允许您同时处理多个条码流以获得最高性能。这在处理内存中的批量条码读取操作时尤其有用。 与基于文件的条形码读取相比,使用内存流有哪些优势? 使用 IronBarcode 进行内存流处理可消除磁盘 I/O 开销,从而提供更快的性能;由于敏感数据从不接触磁盘存储,因此安全性得到了增强;在磁盘访问可能受限或昂贵的云环境中,兼容性更好。 如何从 PDF 文档流中读取 BarCode? IronBarcode 可以从图像和 PDF 文档流中读取条形码。只需将您的 PDF MemoryStream 传递给 BarcodeReader.Read() 方法,它便会提取并解码 PDF 页面中的所有条码。 在使用数据流时,是否可以对条形码读取过程进行微调? 是的,IronBarcode 提供可定制的条码阅读器设置,允许您根据具体使用情况优化读取过程,无论是处理单个数据流还是存储在集合中的多个数据流。 Hairil Hasyimi Bin Omar 立即与工程团队聊天 软件工程师 如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。 准备开始了吗? Nuget 下载 2,108,094 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:2,108,094 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package BarCode 运行示例 观看您的字符串变成 BarCode。 免费 NuGet 下载 总下载量:2,108,094 查看许可证