Cómo Leer Códigos de Barras Desde Objetos del Sistema de Dibujo

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.

Preguntas Frecuentes

¿Cómo puedo leer códigos de barras desde objetos System.Drawing en .NET C#?

Puede leer códigos de barras desde objetos System.Drawing utilizando IronBarcode junto con IronDrawing. Primero, convierta sus objetos System.Drawing en AnyBitmap usando IronDrawing, luego use el método Read de IronBarcode para leer los códigos de barras.

¿Qué es IronDrawing y cómo ayuda con la lectura de códigos de barras?

IronDrawing es una biblioteca gratuita y de código abierto de IronSoftware que permite la conversión implícita de objetos System.Drawing en AnyBitmap. Esto facilita la lectura de códigos de barras en sistemas operativos que no son Windows al hacer que estos objetos sean compatibles con IronBarcode.

¿Puedo usar IronBarcode para leer códigos de barras en MacOS y Linux?

Sí, usando IronDrawing puede convertir objetos System.Drawing en AnyBitmap, lo que permite que IronBarcode lea códigos de barras en MacOS y Linux, superando la limitación de que System.Drawing sea solo para Windows.

¿Qué tipos de objetos de imagen pueden convertirse a AnyBitmap para la lectura de códigos de barras?

Además de objetos System.Drawing, puede convertir System.Drawing.Bitmap, System.Drawing.Image, SkiaSharp.SKBitmap, SkiaSharp.SKImage y objetos de SixLabors.ImageSharp a AnyBitmap para la lectura de códigos de barras con IronBarcode.

¿Cómo muestro los valores de códigos de barras detectados usando IronBarcode?

Después de leer códigos de barras con el método Read de IronBarcode, itere sobre la matriz BarcodeResult e imprima cada valor de código de barras en la consola.

¿Está IronDrawing incluido al instalar una biblioteca de lectura de códigos de barras desde NuGet?

Sí, IronDrawing se incluye automáticamente en su proyecto cuando instala IronBarcode desde NuGet, proporcionando una integración sin fisuras para la lectura de códigos de barras.

¿Cómo ayuda la conversión implícita de imágenes en el procesamiento de códigos de barras?

La conversión implícita de objetos de imagen en AnyBitmap usando IronDrawing simplifica el proceso de hacer que los objetos System.Drawing sean compatibles con IronBarcode, mejorando el procesamiento de códigos de barras en varios sistemas operativos.

Hairil Hasyimi Bin Omar
Ingeniero de Software
Como todos los grandes ingenieros, Hairil es un ávido aprendiz. Está refinando su conocimiento de C#, Python y Java, usando ese conocimiento para agregar valor a los miembros del equipo en Iron Software. Hairil se unió al equipo de Iron Software desde la Universiti Teknologi MARA en Malasia, donde se ...
Leer más
¿Listo para empezar?
Nuget Descargas 1,935,276 | Versión: 2025.11 recién lanzado