How to Read Barcodes From Streams in C#

In this tutorial, we explore how to read barcodes from memory streams in C# using the Iron Barcode library. The process begins with preparing image files and a PDF document containing barcodes. These files are converted into memory streams using classes from IronSoftware.Drawing. The BarcodeReader class then extracts information from these streams. The tutorial demonstrates how to loop through each memory stream object and use Console.WriteLine to display decoded barcode information. Additionally, it covers reading barcodes from PDFs transformed into streams, illustrating the versatility of Iron Barcode in handling different formats. By executing the program, we observe how the code efficiently deciphers and presents barcode data from image and PDF streams.

This tutorial not only teaches how to read barcodes but also encourages further exploration of Iron Barcode's capabilities for integrating these techniques into C# projects. For more resources, viewers are encouraged to subscribe for additional tutorials and try the software firsthand by downloading it from the provided link.

// Import necessary namespaces
using System;
using System.IO;
using IronBarCode;  // Import the Iron Barcode library
using IronSoftware.Drawing;  // Import the drawing library for handling images and PDFs

class BarcodeReaderExample
{
    static void Main()
    {
        // Assuming image files are already available and converted into memory streams
        MemoryStream imageStream = new MemoryStream();
        // Load your image file as a stream (replace "imageFilePath" with the path to your file)
        using (FileStream file = new FileStream("imageFilePath", FileMode.Open, FileAccess.Read))
        {
            file.CopyTo(imageStream);
        }

        // Reset the stream position to start reading from the beginning
        imageStream.Position = 0;

        // Decode barcode from the image stream
        var barcodes = BarcodeReader.Read(imageStream);

        // Loop through each decoded barcode and display the information
        foreach (var barcode in barcodes)
        {
            Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}");
        }

        // Repeat similar steps for PDFs
        MemoryStream pdfStream = new MemoryStream();
        // Load your PDF file as a stream (replace "pdfFilePath" with the path to your file)
        using (FileStream pdfFile = new FileStream("pdfFilePath", FileMode.Open, FileAccess.Read))
        {
            pdfFile.CopyTo(pdfStream);
        }

        // Reset the stream for reading
        pdfStream.Position = 0;

        // Decode barcode from the PDF stream
        var pdfBarcodes = BarcodeReader.ReadPdf(pdfStream);

        // Loop through each decoded barcode from the PDF and display the information
        foreach (var barcode in pdfBarcodes)
        {
            Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}");
        }
    }
}
// Import necessary namespaces
using System;
using System.IO;
using IronBarCode;  // Import the Iron Barcode library
using IronSoftware.Drawing;  // Import the drawing library for handling images and PDFs

class BarcodeReaderExample
{
    static void Main()
    {
        // Assuming image files are already available and converted into memory streams
        MemoryStream imageStream = new MemoryStream();
        // Load your image file as a stream (replace "imageFilePath" with the path to your file)
        using (FileStream file = new FileStream("imageFilePath", FileMode.Open, FileAccess.Read))
        {
            file.CopyTo(imageStream);
        }

        // Reset the stream position to start reading from the beginning
        imageStream.Position = 0;

        // Decode barcode from the image stream
        var barcodes = BarcodeReader.Read(imageStream);

        // Loop through each decoded barcode and display the information
        foreach (var barcode in barcodes)
        {
            Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}");
        }

        // Repeat similar steps for PDFs
        MemoryStream pdfStream = new MemoryStream();
        // Load your PDF file as a stream (replace "pdfFilePath" with the path to your file)
        using (FileStream pdfFile = new FileStream("pdfFilePath", FileMode.Open, FileAccess.Read))
        {
            pdfFile.CopyTo(pdfStream);
        }

        // Reset the stream for reading
        pdfStream.Position = 0;

        // Decode barcode from the PDF stream
        var pdfBarcodes = BarcodeReader.ReadPdf(pdfStream);

        // Loop through each decoded barcode from the PDF and display the information
        foreach (var barcode in pdfBarcodes)
        {
            Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}");
        }
    }
}
' Import necessary namespaces
Imports System
Imports System.IO
Imports IronBarCode ' Import the Iron Barcode library
Imports IronSoftware.Drawing ' Import the drawing library for handling images and PDFs

Friend Class BarcodeReaderExample
	Shared Sub Main()
		' Assuming image files are already available and converted into memory streams
		Dim imageStream As New MemoryStream()
		' Load your image file as a stream (replace "imageFilePath" with the path to your file)
		Using file As New FileStream("imageFilePath", FileMode.Open, FileAccess.Read)
			file.CopyTo(imageStream)
		End Using

		' Reset the stream position to start reading from the beginning
		imageStream.Position = 0

		' Decode barcode from the image stream
		Dim barcodes = BarcodeReader.Read(imageStream)

		' Loop through each decoded barcode and display the information
		For Each barcode In barcodes
			Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}")
		Next barcode

		' Repeat similar steps for PDFs
		Dim pdfStream As New MemoryStream()
		' Load your PDF file as a stream (replace "pdfFilePath" with the path to your file)
		Using pdfFile As New FileStream("pdfFilePath", FileMode.Open, FileAccess.Read)
			pdfFile.CopyTo(pdfStream)
		End Using

		' Reset the stream for reading
		pdfStream.Position = 0

		' Decode barcode from the PDF stream
		Dim pdfBarcodes = BarcodeReader.ReadPdf(pdfStream)

		' Loop through each decoded barcode from the PDF and display the information
		For Each barcode In pdfBarcodes
			Console.WriteLine($"Type: {barcode.BarcodeType}, Value: {barcode.Text}")
		Next barcode
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to read Barcodes from Streams

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 Barcodes From System Drawing Objects
NEXT >
How to Read Barcodes From PDFs in C#