How to Read Barcodes From System Drawing Objects

How to Read Barcodes From System.Drawing Objects

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

System.Drawing objects are widely used in .NET for tasks related to image processing by developers. However, Microsoft has discontinued support for System.Drawing on other operating systems, such as MacOS and Linux, and now exclusively supports Windows. This significant change has caused multiple issues for developers using IronBarcode on operating systems other than Windows. This is because working with barcodes typically involves using objects like graphics, images, and fonts.

To address this problem, we have introduced an alternative solution: IronDrawing. This free and open-source library, initiated by IronSoftware, aims to simplify the process of making it work on operating systems other than Windows. This provides a user-friendly experience for our users. Once you install IronBarcode from NuGet, IronDrawing will be automatically included in your project.

Quickstart: Read a barcode using AnyBitmap in one easy line

This snippet shows how effortlessly IronBarcode can read a barcode by creating a System.Drawing.Bitmap and letting IronDrawing implicitly cast it to AnyBitmap. With just one line, developers on any OS get results fast and simply.

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 results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

Cast System.Drawing to AnyBitmap

Reading barcodes from System.Drawing simply involves casting the object to AnyBitmap. IronDrawing was designed with ease of use in mind. Consequently, IronDrawing supports implicit casting for image objects from System.Drawing into IronSoftware.Drawing image objects called AnyBitmap.

In addition to System.Drawing objects, we also support casting from other types of objects, including:

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

Users can refer to the following code example for casting objects above. Below is a code snippet that demonstrates how to cast images of barcodes from System.Drawing objects into IronSoftware.Drawing.AnyBitmap. Here is a simple example:

: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);
Imports IronSoftware.Drawing
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

' Instantiate System.Drawing.Bitmap
Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")

' Cast from System.Drawing.Bitmap to AnyBitmap
Private barcode1 As AnyBitmap = bitmapFromBitmap

barcodes.Add(barcode1)

' Instantiate System.Drawing.Bitmap
Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")

' Cast from System.Drawing.Image to AnyBitmap
Dim barcode2 As AnyBitmap = bitmapFromFile

barcodes.Add(barcode2)
$vbLabelText   $csharpLabel

From the code snippet above, we loaded two barcode images as System.Drawing.Bitmap and System.Drawing.Image. We then implicitly cast them into AnyBitmap simply by assigning them to AnyBitmap objects. Subsequently, we added these objects to an AnyBitmap list.

Read Barcodes from AnyBitmap

IronBarcode can readily accept IronSoftware.Drawing.AnyBitmap objects in all of its methods without requiring any additional configuration. This offers ease of use to developers who use IronBarcode with System.Drawing objects that are not supported on operating systems other than Windows. The code snippet below demonstrates how to do this.

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

List<AnyBitmap> barcodes = new List<AnyBitmap>();

System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");
AnyBitmap barcode1 = bitmapFromBitmap;
barcodes.Add(barcode1);

System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");
AnyBitmap barcode2 = bitmapFromFile;
barcodes.Add(barcode2);

foreach (var barcode in barcodes)
{
    // Read the barcode
    var results = BarcodeReader.Read(barcode);
    foreach (var result in results)
    {
        // Output the detected barcode value
        Console.WriteLine(result.Value);
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic

Private barcodes As New List(Of AnyBitmap)()

Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")
Private barcode1 As AnyBitmap = bitmapFromBitmap
barcodes.Add(barcode1)

Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")
Dim barcode2 As AnyBitmap = bitmapFromFile
barcodes.Add(barcode2)

For Each barcode In barcodes
	' Read the barcode
	Dim results = BarcodeReader.Read(barcode)
	For Each result In results
		' Output the detected barcode value
		Console.WriteLine(result.Value)
	Next result
Next barcode
$vbLabelText   $csharpLabel

The code snippet above is an extension of the previous one. Once we populated the AnyBitmap list, we iterated through the list and called the Read method on each AnyBitmap object as the parameter, which then returned IronBarcode.BarcodeResults. We then iterated through the returned object to print the barcode value to the console.

The area of functionality in IronSoftware.Drawing is not only limited to casting images. It is also heavily used in other image processing aspects, such as colors and fonts that are useful in styling barcodes and QR codes. Users can explore how we utilize IronDrawing to customize and add logos to QR codes.

常见问题解答

我如何从.NET C#中的System.Drawing对象读取条码?

您可以通过结合使用IronBarcode和IronDrawing,从System.Drawing对象中读取条码。首先,使用IronDrawing将您的System.Drawing对象转换为AnyBitmap,然后使用IronBarcode的Read方法读取条码。

什么是IronDrawing及其在条码读取中的作用?

IronDrawing是铁软件公司提供的一个免费开源库,允许将System.Drawing对象隐式转换为AnyBitmap。这使这些对象可以与IronBarcode兼容,从而在非Windows操作系统上进行条码读取。

我可以使用IronBarcode在MacOS和Linux上读取条码吗?

可以,通过使用IronDrawing,可以将System.Drawing对象转换为AnyBitmap,从而使IronBarcode可以在MacOS和Linux上读取条码,克服System.Drawing仅限于Windows的限制。

哪些图像对象类型可以转换为AnyBitmap以进行条码读取?

除了System.Drawing对象,您可以将System.Drawing.Bitmap、System.Drawing.Image、SkiaSharp.SKBitmap、SkiaSharp.SKImage和SixLabors.ImageSharp对象转换为AnyBitmap,以使用IronBarcode进行条码读取。

如何使用IronBarcode显示检测到的条码值?

在使用IronBarcode的Read方法读取条码后,通过BarcodeResult数组进行迭代,并将每个条码值打印到控制台。

从NuGet安装条码读取库时会包含IronDrawing吗?

会,当您从NuGet中安装IronBarcode时,IronDrawing会自动包含在您的项目中,为条码读取提供无缝集成。

图像对象的隐式转换如何帮助条码处理?

使用IronDrawing将图像对象隐式转换为AnyBitmap简化了使System.Drawing对象与IronBarcode兼容的过程,增强了在各种操作系统上进行条码处理的能力。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布