How to Read Barcodes From System Drawing Objects

In this video tutorial, we delve into the process of reading barcodes using the Iron Barcode Library in conjunction with System.Drawing objects. The session begins by ensuring the Iron Barcode Library is installed via the NuGet package manager. Moving on to the Program.cs file, we import essential namespaces: Iron Barcode for barcode functionality and Iron Software.Drawing for image handling.

We then initialize a list of AnyBitmap to hold the barcodes, loading a barcode image as a System.Drawing.Bitmap and casting it into an AnyBitmap object. This is followed by adding the image to the barcode list. Similarly, a QR code image is loaded and added to the list. Iterating through the list, each barcode is read using the ReadBarcode method, which returns detected barcodes that are printed on the console.

The tutorial showcases the ease of using Iron Barcode, highlighting its seamless integration with System.Drawing objects and its support for implicit casting. By the end of the tutorial, the program successfully detects and prints the values of the barcodes and QR codes, with each value corresponding to the data encoded within. The tutorial concludes with an invitation to the audience to try out the software by downloading it from the website.

using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace BarcodeReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // List to hold AnyBitmap objects containing barcode images
            List<AnyBitmap> barcodeImages = new List<AnyBitmap>();

            // Load a barcode image and convert it to AnyBitmap
            Bitmap barcodeImage = (Bitmap)Image.FromFile("barcode.png");
            AnyBitmap anyBitmapBarcode = (AnyBitmap)barcodeImage;
            barcodeImages.Add(anyBitmapBarcode);

            // Load a QR code image and convert it to AnyBitmap
            Bitmap qrCodeImage = (Bitmap)Image.FromFile("qrcode.png");
            AnyBitmap anyBitmapQRCode = (AnyBitmap)qrCodeImage;
            barcodeImages.Add(anyBitmapQRCode);

            // Iterate through the list of AnyBitmap images and read barcodes
            foreach (AnyBitmap bitmap in barcodeImages)
            {
                // Read the barcode from the image
                var barcodes = BarcodeReader.ReadBarcodes(bitmap);

                // Output each barcode and its type
                foreach (var barcode in barcodes)
                {
                    Console.WriteLine($"Detected Barcode: {barcode.Text}, Type: {barcode.BarcodeFormat}");
                }
            }
        }
    }
}
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace BarcodeReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // List to hold AnyBitmap objects containing barcode images
            List<AnyBitmap> barcodeImages = new List<AnyBitmap>();

            // Load a barcode image and convert it to AnyBitmap
            Bitmap barcodeImage = (Bitmap)Image.FromFile("barcode.png");
            AnyBitmap anyBitmapBarcode = (AnyBitmap)barcodeImage;
            barcodeImages.Add(anyBitmapBarcode);

            // Load a QR code image and convert it to AnyBitmap
            Bitmap qrCodeImage = (Bitmap)Image.FromFile("qrcode.png");
            AnyBitmap anyBitmapQRCode = (AnyBitmap)qrCodeImage;
            barcodeImages.Add(anyBitmapQRCode);

            // Iterate through the list of AnyBitmap images and read barcodes
            foreach (AnyBitmap bitmap in barcodeImages)
            {
                // Read the barcode from the image
                var barcodes = BarcodeReader.ReadBarcodes(bitmap);

                // Output each barcode and its type
                foreach (var barcode in barcodes)
                {
                    Console.WriteLine($"Detected Barcode: {barcode.Text}, Type: {barcode.BarcodeFormat}");
                }
            }
        }
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.Drawing

Namespace BarcodeReaderExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' List to hold AnyBitmap objects containing barcode images
			Dim barcodeImages As New List(Of AnyBitmap)()

			' Load a barcode image and convert it to AnyBitmap
			Dim barcodeImage As Bitmap = CType(Image.FromFile("barcode.png"), Bitmap)
			Dim anyBitmapBarcode As AnyBitmap = CType(barcodeImage, AnyBitmap)
			barcodeImages.Add(anyBitmapBarcode)

			' Load a QR code image and convert it to AnyBitmap
			Dim qrCodeImage As Bitmap = CType(Image.FromFile("qrcode.png"), Bitmap)
			Dim anyBitmapQRCode As AnyBitmap = CType(qrCodeImage, AnyBitmap)
			barcodeImages.Add(anyBitmapQRCode)

			' Iterate through the list of AnyBitmap images and read barcodes
			For Each bitmap As AnyBitmap In barcodeImages
				' Read the barcode from the image
				Dim barcodes = BarcodeReader.ReadBarcodes(bitmap)

				' Output each barcode and its type
				For Each barcode In barcodes
					Console.WriteLine($"Detected Barcode: {barcode.Text}, Type: {barcode.BarcodeFormat}")
				Next barcode
			Next bitmap
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Further Reading: How to Read Barcodes From System.Drawing Objects

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Read Multiple Barcodes at Once in C#
NEXT >
How to Read Barcodes From Streams in C#