How to Read Barcodes From System.Drawing Objects

by Hairil Hasyimi Bin Omar

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.

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

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

First Step:
green arrow pointer

Check out IronBarcode on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming with C#.

C# NuGet Library for nuget.org/packages/BarCode/
Install-Package BarCode

Consider installing the IronBarcode DLL directly. Download and manually install it for your project or GAC form: IronBarCode.zip

Dll Img related to How to Read Barcodes From System.Drawing Objects

Manually install into your project

Download DLL

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.

: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)
VB   C#

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
VB   C#

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.

Hairil related to Read Barcodes from AnyBitmap

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.