IRONSOFTWAREHOME

How to Read Barcodes From System.Drawing in C#

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: 2026年6月4日

IronBarcode允许从System.Drawing支持限制。

简介

System.Drawing对象在.NET中广泛用于图像处理任务。 然而,微软已经停止对System.DrawingMacOSLinux上的支持,现在仅支持Windows。 这一变化给在非 Windows 操作系统上使用 IronBarcode 的开发人员带来了问题,因为处理条形码通常涉及 图形图像字体

为了解决这个问题,我们引入了IronDrawing。 这款免费开源的库由Iron Software创建,简化了跨平台支持并提供无缝体验。 当您从NuGet安装IronBarcode时,IronDrawing会自动包含在您的项目中。

对于刚刚接触条形码阅读的开发人员,请参阅我们全面的 阅读条形码教程,其中涵盖基本概念和基本使用模式。 如果您正在使用各种图像格式,我们的从图像中读取 BarCode 指南提供了更多的上下文和示例。

AnyBitmap读取条码仅需一步@@--AH2EG--@@

此代码片段展示了IronBarcode如何通过创建AnyBitmap。 只需一行,任何操作系统上的开发人员都能快速获得结果。

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. 2复制并运行这段代码。

    var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronBarcode
    arrow pointer

如何将AnyBitmap?

AnyBitmapAnyBitmap

除了System.Drawing对象,我们还支持从其他类型转换:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SkiaSharp.SKImage
  • SixLabors.ImageSharp

请参阅此代码示例来铸造上述对象。 下面演示了如何将条码图像从IronSoftware.Drawing.AnyBitmap

哪些System.Drawing类型可以被转换?

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);

此代码演示了通过IronBarcode之间的无缝集成。 这种兼容性适用于各种条形码格式,详见我们的支持的条形码格式指南,包括 QR 码、Code 128、Code 39 和许多其他条形码格式。

隐式铸造为何有效?

在上面的代码中,我们将两个条码图像加载为System.Drawing.Image。 然后我们通过将它们赋值给AnyBitmap列表中。

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
}

哪些是常见的铸造错误?

在将AnyBitmap时,开发人员可能会遇到:

  1. 空引用异常:在转换之前确保您的System.Drawing对象不是空的 2.不支持的格式异常:某些特殊的图像格式需要预先转换。 3.内存问题:大图片需要适当的处理模式

对于铸造问题的故障排除,我们的故障排除指南提供了条形码识别过程中常见问题的解决方案。

如何从AnyBitmap对象读取条码?

IronBarcode接受**IronSoftware.Drawing.AnyBitmap**对象在所有方法中无需额外配置。 这简化了在非Windows操作系统上使用System.Drawing对象的开发。 以下代码演示了这一点:

哪些方法接受AnyBitmap参数?

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);
    }
}

除了基本的AnyBitmap参数。 有关高级应用场景,请参阅我们的读取多个条形码指南,该指南演示了对单个图像中多个条形码的高效处理:

// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
    // Specify barcode types to search for
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // Set confidence threshold
    ConfidenceThreshold = 0.95
};

// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
C#

如何处理多个 BarCode 结果?

上面的代码扩展了前面的示例。 在填充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}");
}

我可以使用哪些其他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);

对于需要进行特定图像校正的场景,我们的图像校正指南详细介绍了如何使用过滤器来增强条形码的可读性。

为什么选择System.Drawing

System.Drawing的引人注目的优势:

  1. 跨平台支持:与仅在.NET Core/5+上支持Windows的System.Drawing不同,在Windows、Linux和macOS上无缝运行
  2. 现代架构:基于ImageSharp构建,以提高性能和内存管理
  3. 简化的API:在维持熟悉的System.Drawing类似界面的同时添加了现代便利功能
  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,并读取图像中存在的所有条码。

Is IronDrawing included when I install IronBarcode?

Yes, when you install IronBarcode from NuGet, IronDrawing is automatically included, ensuring seamless integration for cross-platform barcode scanning.

How do I handle multiple barcodes in a single image using IronBarcode?

Use the BarcodeReader.Read method with AnyBitmap, and leverage parallel processing to efficiently handle and read multiple barcodes in a single image.

What image processing features does IronDrawing provide?

IronDrawing can handle image processing tasks such as color adjustment and font handling, which are useful when styling barcodes and QR codes.

How does implicit casting help in cross-platform development?

Implicit casting in IronDrawing allows developers to convert System.Drawing objects to AnyBitmap without changing existing code, facilitating cross-platform compatibility and development for Linux, macOS, and Windows.

准备开始了吗?

Nuget Downloads 2,422,100版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 "IronBarcode"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并将 IronBarCode 解压到解决方案目录中的 ~/Libs 等位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择浏览,"IronBarcode.dll"

许可 $999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户