HOWTO · Csharp

How to Read Barcode in C#

This article briefly discusses reading Barcode from an image using C#.

On this page

This tutorial explains how to read barcode data from an image file in C#. It presents a complete program that uses a third-party package to find one or more barcodes and print their symbology and decoded text. The demonstrated workflow reads a local image file; adapt the input step separately if your barcode arrives through another source.

Read Barcode in C#

You can read barcode images in C# with the Aspose.BarCode assembly package. Create a console project in Visual Studio, install the package from NuGet.org, and place barcode.png in the project directory so the sample can open it by name. The package supplies the recognition API; the image remains your application input.

The recognition API is in the Aspose.BarCode.BarCodeRecognition namespace. Create a BarCodeReader, call its ReadBarCodes() method, and inspect each returned BarCodeResult. Each result exposes CodeType for the symbology and CodeText for the decoded value, so the same loop can handle multiple barcodes in one image.

The example below uses the file-name constructor. It keeps input and output responsibilities visible while disposing the reader after use. This library-dependent sample is not presented as executed evidence here: install the package and provide barcode.png before running it. If the file cannot be opened or the library cannot decode it, the try/catch block writes the library’s message instead of ending without context.

  • Create a C# console application in Visual Studio.
  • Select Manage NuGet Packages in the Project tab

  • Browse for the Aspose.BarCode package from NuGet.org and install it.

    Install the Aspose.BarCode package from NuGet.org

    Now, you can import the Aspose.BarCode.BarCodeRecognition namespace in your code to use the barcode recognition functionality.

  • Copy the following code snippet to your program.cs file
    using System;
    using Aspose.BarCode;
    using Aspose.BarCode.BarCodeRecognition;
    
    namespace BarcodeScanner {
      class Program {
        static void Main(string[] args) {
          try {
            // The file is in the project directory.
            using (BarCodeReader barCodeReader = new BarCodeReader("barcode.png")) {
              foreach (BarCodeResult output in barCodeReader.ReadBarCodes()) {
                // Read the barcode
                Console.WriteLine("Symbology Type: " + output.CodeType);
                Console.WriteLine("CodeText: " + output.CodeText);
              }
            }
          } catch (Exception ex) {
            Console.WriteLine(ex.Message);
          }
        }
      }
    }
    

    This code reads barcode.png from the project directory. The foreach loop processes every BarCodeResult returned for the image. For each result, it prints the symbology from CodeType and the decoded value from CodeText. The try/catch block keeps an input or library error from terminating the sample without a message.

    The BarCodeReader class has overloaded constructors, and this example deliberately uses the constructor that takes a file name. Other input forms depend on the library version and are outside the demonstrated workflow, so this article does not present them as interchangeable code.

The following image is the input used by the article’s illustration.

Barcode image used as input

The reader reports the decoded values in the application output. The supplied screenshot shows the result for this input.

Decoded barcode output

For a quick view of the output shape, this self-contained console sample uses a local result object and prints the same two fields as the library example.

using System;

class Program
{
    static void Main()
    {
        var result = new BarcodeResult("Code128", "123456789");
        Console.WriteLine("Symbology Type: " + result.CodeType);
        Console.WriteLine("CodeText: " + result.CodeText);
    }

    private sealed class BarcodeResult
    {
        public BarcodeResult(string codeType, string codeText)
        {
            CodeType = codeType;
            CodeText = codeText;
        }

        public string CodeType { get; }
        public string CodeText { get; }
    }
}
Symbology Type: Code128
CodeText: 123456789

The Aspose.BarCode reader is a paid tool, and a license is required for unrestricted functionality.