Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green 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; // Required for AnyBitmap class
using System.Collections.Generic; // Required for using the List<T> class
using System.Drawing; // Required for using Bitmap and Image classes

// Create a list to store AnyBitmap objects which will represent barcodes
List<AnyBitmap> barcodes = new List<AnyBitmap>();

// Instantiate a Bitmap object from an image file
// Replace "test1.jpg" with the actual path to your image file
Bitmap bitmapFromBitmap = new Bitmap("test1.jpg");

// Convert the Bitmap to AnyBitmap using a defined method
// AnyBitmap allows additional functionalities specific to IronSoftware
AnyBitmap barcode1 = AnyBitmap.FromBitmap(bitmapFromBitmap);

// Add the converted AnyBitmap object to the list of barcodes
barcodes.Add(barcode1);

// Instantiate an Image object from another image file
// Replace "test2.png" with the actual path to your image file
Image bitmapFromFile = Image.FromFile("test2.png");

// Convert the Image to AnyBitmap using a defined method
AnyBitmap barcode2 = AnyBitmap.FromImage(bitmapFromFile);

// Add the second converted AnyBitmap object to the list of barcodes
barcodes.Add(barcode2);
Imports IronSoftware.Drawing ' Required for AnyBitmap class

Imports System.Collections.Generic ' Required for using the List<T> class

Imports System.Drawing ' Required for using Bitmap and Image classes



' Create a list to store AnyBitmap objects which will represent barcodes

Private barcodes As New List(Of AnyBitmap)()



' Instantiate a Bitmap object from an image file

' Replace "test1.jpg" with the actual path to your image file

Private bitmapFromBitmap As New Bitmap("test1.jpg")



' Convert the Bitmap to AnyBitmap using a defined method

' AnyBitmap allows additional functionalities specific to IronSoftware

Private barcode1 As AnyBitmap = AnyBitmap.FromBitmap(bitmapFromBitmap)



' Add the converted AnyBitmap object to the list of barcodes

barcodes.Add(barcode1)



' Instantiate an Image object from another image file

' Replace "test2.png" with the actual path to your image file

Dim bitmapFromFile As Image = Image.FromFile("test2.png")



' Convert the Image to AnyBitmap using a defined method

Dim barcode2 As AnyBitmap = AnyBitmap.FromImage(bitmapFromFile)



' Add the second converted AnyBitmap object to the list of barcodes

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; // Include the IronBarCode library for barcode reading
using IronSoftware.Drawing; // Include IronSoftware.Drawing for handling AnyBitmap type
using System;
using System.Collections.Generic;
using System.Drawing; // Include System.Drawing for handling bitmap and image operations

// Create a list to store barcode images of type AnyBitmap
List<AnyBitmap> barcodes = new List<AnyBitmap>();

try
{
    // Load a bitmap from a jpg file, convert it to AnyBitmap, and add it to the list
    using (Bitmap bitmapFromBitmap = new Bitmap("test1.jpg"))
    {
        AnyBitmap barcode1 = AnyBitmap.FromBitmap(bitmapFromBitmap);
        barcodes.Add(barcode1);
    }

    // Load an image from a png file, convert it to AnyBitmap, and add it to the list
    using (Image bitmapFromFile = Image.FromFile("test2.png"))
    {
        AnyBitmap barcode2 = AnyBitmap.FromImage(bitmapFromFile);
        barcodes.Add(barcode2);
    }
}
catch (Exception ex)
{
    // Handle exceptions, such as file not found or incompatible format
    Console.WriteLine($"An error occurred while loading images: {ex.Message}");
}

// Iterate through the list of barcodes and process each one
foreach (var barcode in barcodes)
{
    try
    {
        // Read the barcodes using the BarcodeReader
        var results = BarcodeReader.Read(barcode);
        foreach (var result in results)
        {
            // Output the detected barcode value to the console
            Console.WriteLine(result.Value);
        }
    }
    catch (Exception ex)
    {
        // Handle exceptions during barcode reading
        Console.WriteLine($"An error occurred while reading barcodes: {ex.Message}");
    }
}
Imports IronBarCode ' Include the IronBarCode library for barcode reading

Imports IronSoftware.Drawing ' Include IronSoftware.Drawing for handling AnyBitmap type

Imports System

Imports System.Collections.Generic

Imports System.Drawing ' Include System.Drawing for handling bitmap and image operations



' Create a list to store barcode images of type AnyBitmap

Private barcodes As New List(Of AnyBitmap)()



Try

	' Load a bitmap from a jpg file, convert it to AnyBitmap, and add it to the list

	Using bitmapFromBitmap As New Bitmap("test1.jpg")

		Dim barcode1 As AnyBitmap = AnyBitmap.FromBitmap(bitmapFromBitmap)

		barcodes.Add(barcode1)

	End Using



	' Load an image from a png file, convert it to AnyBitmap, and add it to the list

	Using bitmapFromFile As Image = Image.FromFile("test2.png")

		Dim barcode2 As AnyBitmap = AnyBitmap.FromImage(bitmapFromFile)

		barcodes.Add(barcode2)

	End Using

Catch ex As Exception

	' Handle exceptions, such as file not found or incompatible format

	Console.WriteLine($"An error occurred while loading images: {ex.Message}")

End Try



' Iterate through the list of barcodes and process each one

For Each barcode In barcodes

	Try

		' Read the barcodes using the BarcodeReader

		Dim results = BarcodeReader.Read(barcode)

		For Each result In results

			' Output the detected barcode value to the console

			Console.WriteLine(result.Value)

		Next result

	Catch ex As Exception

		' Handle exceptions during barcode reading

		Console.WriteLine($"An error occurred while reading barcodes: {ex.Message}")

	End Try

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.

Frequently Asked Questions

What is the alternative to System.Drawing for reading barcodes on non-Windows operating systems?

The alternative to System.Drawing for reading barcodes on non-Windows operating systems is IronDrawing, a free and open-source library initiated by IronSoftware.

How do you cast System.Drawing objects to AnyBitmap using IronDrawing?

You can cast System.Drawing objects to AnyBitmap in IronDrawing by utilizing implicit casting. This allows you to convert image objects from System.Drawing into IronSoftware.Drawing image objects called AnyBitmap.

Can IronBarcode read barcodes from AnyBitmap objects?

Yes, IronBarcode can readily accept IronSoftware.Drawing.AnyBitmap objects in all of its methods without requiring any additional configuration.

What types of System.Drawing objects can be cast to AnyBitmap?

In addition to System.Drawing objects, you can cast System.Drawing.Bitmap, System.Drawing.Image, SkiaSharp.SKBitmap, SkiaSharp.SKImage, and SixLabors.ImageSharp objects to AnyBitmap.

What is the purpose of using IronDrawing with IronBarcode?

The purpose of using IronDrawing with IronBarcode is to enable barcode reading from System.Drawing objects on operating systems other than Windows, providing a user-friendly experience and broader compatibility.

Is IronDrawing included automatically when installing IronBarcode from NuGet?

Yes, once you install IronBarcode from NuGet, IronDrawing will be automatically included in your project.

How can developers display detected barcode values using IronBarcode?

Developers can display detected barcode values by iterating through the BarcodeResult array returned by the Read method and printing each barcode value to the console.

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.