IronBarcode 操作指南 从 System.Drawing 中读取条码 How to Read Barcodes From System.Drawing in C Hairil Hasyimi Bin Omar 已更新:2026年2月20日 下载 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能够通过自动将 System.Drawing 对象转换为 AnyBitmap 到 IronDrawing,在所有操作系统上读取条形码,从而解决了 Microsoft 对 System.Drawing 支持的 Windows 限制。 简介 System.Drawing 对象在.NET中广泛用于图像处理任务。 然而,微软已停止对MacOS和Linux上的System.Drawing 的支持,现在仅支持Windows 。 这一变化给在非 Windows 操作系统上使用 IronBarcode 的开发人员带来了问题,因为处理条形码通常涉及 图形、图像 和 字体。 为了解决这个问题,我们引入了IronDrawing。 这个由 IronSoftware 创建的免费和开源库简化了跨平台支持并提供了无缝体验。 当您从NuGet安装IronBarcode时,IronDrawing 会自动包含在您的项目中。 对于刚刚接触条形码阅读的开发人员,请参阅我们全面的 阅读条形码教程,其中涵盖基本概念和基本使用模式。 如果您正在使用各种图像格式,我们的从图像中读取 BarCode 指南提供了更多的上下文和示例。 快速入门:使用 CODE-848 一行轻松读取条形码。 此代码片段展示了IronBarcode如何通过创建 System.Drawing.Bitmap 并让 IronDrawing 将其隐式转换为 AnyBitmap 来读取条形码。 只需一行,任何操作系统上的开发人员都能快速获得结果。 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode PM > Install-Package BarCode 复制并运行这段代码。 var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"))); 部署到您的生产环境中进行测试 通过免费试用立即在您的项目中开始使用IronBarcode Free 30 Day Trial 最小工作流程(5 个步骤) 从`System.Drawing`下载用于读取条形码的 C# 库 Utilize `IronDrawing` to cast `System.Drawing` objects into `AnyBitmap` Use the `Read` method to read barcodes from `AnyBitmap` objects 在控制台上显示检测到的条码值 Explore another article to learn how `IronDrawing` is used for handling color and fonts 如何将 System.Drawing 对象转换为 AnyBitmap? 从 System.Drawing 读取条形码需要将对象强制转换为 AnyBitmap。 IronDrawing 的设计旨在方便使用,并支持将 System.Drawing 中的图像对象隐式转换为 IronSoftware.Drawing 中的图像对象,称为 AnyBitmap。 除了 System.Drawing 对象之外,我们也支持从其他类型进行类型转换: System.Drawing.Bitmap System.Drawing.Image SkiaSharp.SKBitmap SkiaSharp.SKImage SixLabors.ImageSharp 请参阅此代码示例来铸造上述对象。 下面演示如何将 System.Drawing 对象中的条形码图像转换为 IronSoftware.Drawing.AnyBitmap: 哪些 System.Drawing 类型可以进行强制转换? :path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.cs using IronSoftware.Drawing; using System.Collections.Generic; List<AnyBitmap> barcodes = new List<AnyBitmap>(); // Instantiate System.Drawing.Bitmap System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg"); // Cast from System.Drawing.Bitmap to AnyBitmap AnyBitmap barcode1 = bitmapFromBitmap; barcodes.Add(barcode1); // Instantiate System.Drawing.Bitmap System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png"); // Cast from System.Drawing.Image to AnyBitmap AnyBitmap barcode2 = bitmapFromFile; barcodes.Add(barcode2); $vbLabelText $csharpLabel 此代码演示了 System.Drawing 对象与 IronBarcode 通过 IronDrawing 之间的无缝集成。 这种兼容性适用于各种条形码格式,详见我们的支持的条形码格式指南,包括 QR 码、Code 128、Code 39 和许多其他条形码格式。 隐式铸造为何有效? 在上面的代码中,我们加载了两个条形码图像,分别为 System.Drawing.Bitmap 和 System.Drawing.Image。 然后,我们将它们隐式转换为 AnyBitmap,方法是将它们赋值给 AnyBitmap 对象,然后将这些对象添加到 AnyBitmap 列表中。 IronDrawing 的隐式类型转换机制使用运算符重载,在 System.Drawing 类型和 AnyBitmap 类型之间提供透明转换。 这种设计模式可以让开发人员维护现有代码,同时获得跨平台兼容性。 转换时要保留所有图像属性,包括分辨率、色深和像素数据,确保无质量损失。 何时应使用显式铸造与隐式铸造? 虽然隐式转换提供了便利,但在某些情况下显式转换可能更受欢迎: // Implicit casting - clean and simple for straightforward conversions System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png"); AnyBitmap anyBitmap = systemBitmap; // Implicit cast // Explicit casting - useful when type clarity is important System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg"); AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast // When working with nullable types or conditional logic System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource(); if (nullableBitmap != null) { AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity // Process the barcode } // Implicit casting - clean and simple for straightforward conversions System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png"); AnyBitmap anyBitmap = systemBitmap; // Implicit cast // Explicit casting - useful when type clarity is important System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg"); AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast // When working with nullable types or conditional logic System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource(); if (nullableBitmap != null) { AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity // Process the barcode } $vbLabelText $csharpLabel 哪些是常见的铸造错误? 将 System.Drawing 转换为 AnyBitmap 时,开发人员可能会遇到以下问题: 1.空引用异常:在进行类型转换之前,请验证您的 System.Drawing 对象是否为空。 2.不支持的格式异常:某些特殊的图像格式需要预先转换。 3.内存问题:大图片需要适当的处理模式 对于铸造问题的故障排除,我们的故障排除指南提供了条形码识别过程中常见问题的解决方案。 如何从 AnyBitmap 对象中读取条形码? IronBarcode在所有方法中都接受IronSoftware.Drawing.AnyBitmap对象,无需额外配置。 这样可以简化在非 Windows 操作系统上使用 System.Drawing 对象时的开发。 以下代码演示了这一点: 哪些方法接受 AnyBitmap 参数? :path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.cs using IronBarCode; using IronSoftware.Drawing; using System; using System.Collections.Generic; // Create a list of image file paths to read barcodes from List<string> barcodeFiles = new List<string> { "test1.jpg", "test2.png" }; foreach (var barcodeFile in barcodeFiles) { // Read the barcode from file path var results = BarcodeReader.Read(barcodeFile); foreach (var result in results) { // Output the detected barcode value Console.WriteLine(result.Value); } } $vbLabelText $csharpLabel 除了基本的 Read 方法之外, IronBarcode还提供了几个接受 AnyBitmap 参数的方法。 有关高级应用场景,请参阅我们的读取多个条形码指南,该指南演示了对单个图像中多个条形码的高效处理: // Advanced barcode reading with options var readerOptions = new BarcodeReaderOptions { // Specify barcode types to search for ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Enable machine learning for better accuracy UseML = true, // Set confidence threshold Confidence = 0.95 }; // Read with specific options var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions); // Advanced barcode reading with options var readerOptions = new BarcodeReaderOptions { // Specify barcode types to search for ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Enable machine learning for better accuracy UseML = true, // Set confidence threshold Confidence = 0.95 }; // Read with specific options var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions); $vbLabelText $csharpLabel 如何处理多个 BarCode 结果? 上面的代码扩展了前面的示例。 在填充 AnyBitmap 列表后,我们遍历该列表,并对每个 AnyBitmap 对象调用 Read 方法,该方法返回IronBarCode.BarcodeResults 。 然后,我们对结果进行迭代,将 BarCode 值打印到控制台。 处理多个 BarCode 时,利用并行处理提高性能: // Parallel processing for multiple barcode images var barcodeFiles = Directory.GetFiles("barcodes/", "*.png"); var allResults = new ConcurrentBag<BarcodeResult>(); Parallel.ForEach(barcodeFiles, file => { var bitmap = new System.Drawing.Bitmap(file); var anyBitmap = (AnyBitmap)bitmap; var results = BarcodeReader.Read(anyBitmap); foreach (var result in results) { allResults.Add(result); } bitmap.Dispose(); // Clean up resources }); // Process all results foreach (var result in allResults) { Console.WriteLine($"Found {result.BarcodeType}: {result.Value}"); } // Parallel processing for multiple barcode images var barcodeFiles = Directory.GetFiles("barcodes/", "*.png"); var allResults = new ConcurrentBag<BarcodeResult>(); Parallel.ForEach(barcodeFiles, file => { var bitmap = new System.Drawing.Bitmap(file); var anyBitmap = (AnyBitmap)bitmap; var results = BarcodeReader.Read(anyBitmap); foreach (var result in results) { allResults.Add(result); } bitmap.Dispose(); // Clean up resources }); // Process all results foreach (var result in allResults) { Console.WriteLine($"Found {result.BarcodeType}: {result.Value}"); } $vbLabelText $csharpLabel 我还能使用哪些其他 IronDrawing 功能? IronSoftware.Drawing 功能不仅限于图像投射。 它可以处理图像处理方面的问题,如 颜色和 字体,这对条形码和二维码的样式设计非常有用。 探索我们如何使用 IronDrawing 来定制二维码并添加徽标。 IronDrawing 提供强大的图像处理功能,是对条形码处理的补充: // Using IronDrawing for image preprocessing using IronSoftware.Drawing; // Load and preprocess an image before barcode reading AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg"); // Apply image filters to improve barcode readability preprocessedImage = preprocessedImage.ToGrayScale(); preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image // Read the preprocessed barcode var improvedResults = BarcodeReader.Read(preprocessedImage); // Using IronDrawing for image preprocessing using IronSoftware.Drawing; // Load and preprocess an image before barcode reading AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg"); // Apply image filters to improve barcode readability preprocessedImage = preprocessedImage.ToGrayScale(); preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image // Read the preprocessed barcode var improvedResults = BarcodeReader.Read(preprocessedImage); $vbLabelText $csharpLabel 对于需要进行特定图像校正的场景,我们的图像校正指南详细介绍了如何使用过滤器来增强条形码的可读性。 为什么选择 IronDrawing 而不是 System.Drawing? IronDrawing 比 System.Drawing 具有显著优势: 1.跨平台支持:与 System.Drawing(仅限 Windows 版.NET Core/5+)不同,可在 Windows、Linux 和 macOS 上无缝运行。 2.现代架构:基于 SkiaSharp 和 ImageSharp 构建,以实现更佳的性能和内存管理 3.简化的 API :在保持熟悉的 System.Drawing-like 接口的同时,增加现代化的便利功能 4.积极开发:定期更新和改进,与处于维护模式的 System.Drawing 不同 5.更佳的集成性:专为与 Iron Software 产品实现最佳性能而设计 对于部署注意事项,特别是云环境,请参阅我们关于部署到 Azure和部署到 AWS 的指南,其中包含有关使用 IronDrawing 实现跨平台兼容性的具体说明。 无论是构建桌面应用程序、Web 服务还是云原生解决方案,IronDrawing 都能确保您的条形码处理代码在所有平台上保持可移植性和高效性,使其成为现代.NET开发的理想选择。 常见问题解答 如何在非 Windows 平台上从 System.Drawing 对象读取 BarCode? IronBarcode 通过 IronDrawing 自动处理来自 System.Drawing 对象的跨平台条形码读取,并将其转换为 AnyBitmap 格式。这就解决了 Microsoft System.Drawing 仅限于 Windows 的限制,使您可以在 MacOS 和 Linux 系统上无缝读取条形码。 什么是 IronDrawing,为什么它包含在条形码读取中? IronDrawing 是 Iron Software 创建的免费开源库,为图形操作提供跨平台支持。从 NuGet 安装 IronBarcode 时会自动包含该库,通过将 System.Drawing 对象转换为兼容的 AnyBitmap 格式,可以在所有操作系统上从 System.Drawing 对象读取条形码。 如何转换 System.Drawing.Bitmap 以从中读取条形码? 您可以通过简单的投向 AnyBitmap 从 System.Drawing.Bitmap 读取条形码:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.IronBarcode 通过 IronDrawing 的隐式转换功能自动处理转换。 能否在 Linux 和 MacOS 上使用 System.Drawing 阅读 BarCode? 是的,IronBarcode 可通过 IronDrawing 从 Linux 和 MacOS 上的 System.Drawing 对象读取条形码,IronDrawing 可自动将 System.Drawing 对象转换为跨平台的 AnyBitmap 格式。这克服了微软对 System.Drawing 支持的 Windows 限制。 哪些类型的 System.Drawing 对象可用于条形码读取? IronBarcode 支持从各种 System.Drawing 对象(包括 System.Drawing.Bitmap 和其他图像类型)读取条形码。通过 IronDrawing 的隐式铸造功能,这些对象会自动转换为 AnyBitmap,从而实现跨平台条形码扫描功能。 是否有简单的单行解决方案来从 System.Drawing 读取 BarCode? 是的,IronBarcode 提供了单行解决方案:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.这一行创建了一个 System.Drawing.Bitmap,通过 IronDrawing 将其转换为 AnyBitmap,并读取图像中存在的所有条码。 Hairil Hasyimi Bin Omar 立即与工程团队聊天 软件工程师 如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。 准备开始了吗? Nuget 下载 2,121,847 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:2,121,847 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package BarCode 运行示例 观看您的字符串变成 BarCode。 免费 NuGet 下载 总下载量:2,121,847 查看许可证