Read Barcode from Image in C#

Are you looking for a way to read barcodes or QR codes from images programmatically? If you are a developer, and you need to create your own barcode reader application? You’ve come to the right place. Barcodes are crucial to the accurate tracking of inventory and delivery of products from manufacturing to point of sale. We can easily detect, recognize, and read different types of barcodes and QR codes in .NET applications. In this article, we’ll show you how to read a barcode from an image in C# by following just a few simple steps. You can use this article as a step-by-step guide for developing barcode reader or scanner applications.

Firstly, we’ll take a look at the C# Barcode Reader API to read the barcodes available in the input image. Next, we’ll walk through the steps of how to detect, recognize and extract barcode data from the image. You will find detailed steps and code snippets. Finally, we’ll provide useful links for further enhancements. So let’s get started!

The following topics will be covered in this article:

  1. C# API to Read Barcode from Image
  2. Steps to Read Barcode from an Image
  3. Read Barcode from Bitmap
  4. Read Barcode from Image using Stream
  5. How to Read Barcode of Specific Type
  6. Read Barcodes of Multiple Types from an Image
  7. Detect and Read Predefined Sets of Symbologies
  8. Detect and Read Multiple Barcodes from an Image
  9. Get X and Y Coordinates of Barcode
  10. Read Barcode From Specific Region of Image
  11. Read Barcode From Multiple Regions of Image

C# API to Read Barcode from Image

For reading a barcode from an image, we will be using the Aspose.BarCode for .NET API. The API allows generating, scanning, and reading a wide range of barcode symbologies. It enables rendering barcode images in JPEG, TIFF, PNG, BMP, and GIF format.

The API provides the BarCodeReader class that enables the recognition of more than 60 different barcode types from given images. The first step in detecting barcodes is to specify the source of an image with barcodes. This can be a file, a bitmap object, or a stream. It is then necessary to specify target symbologies within the DecodeType parameter. We can look over all the different types of supported symbologies by specifying the DecodeType.AllSupportedTypes. The ReadBarCodes() method of this class returns an array of recognized barcodes. The BarCodeResult class of the API stores the recognized barcode data such as barcode type, code text, region, and other parameters.

The API also allows specifying the regions of an image the barcode reader should read. This can be accomplished using a .NET Rectangle object and allows avoiding the need to find barcodes in image areas that contain no barcodes by default.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.BarCode 

Read Barcode from an Image using C#

We can easily read a barcode from an image by following the steps given below:

  1. Firstly, load an image using the BarCodeReader class.
  2. Call the readBarCodes() method and get recognition results in BarCodeResult class object.
  3. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from an image in C#.

Read Barcode from an Image.

Read barcode from an image in C#.

Read Barcode from Bitmap in C#

We can easily read a barcode from an image by following the steps given below:

  1. Firstly, load an image using the Bitmap class.
  2. Next, create an instance of the BarCodeReader class with Bitmap object.
  3. Call the ReadBarCodes() method and get recognition results in BarCodeResult class object.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from bitmap in C#.

Read Barcode from Bitmap Image in C#.

Read barcode from Bitmap in C#.

Read Barcode from Image using Stream in C#

We can also load the barcode image using a file stream and read a barcode by following the steps given below:

  1. Firstly, load an image using the FileStream class.
  2. Next, create an instance of the BarCodeReader class with stream object.
  3. Call the ReadBarCodes() method and get recognition results in BarCodeResult class object.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from image using Stream in C#.

Read Barcode of Specific Type from Image in C#

It is recommended to select target barcode symbologies that will be considered for recognition in order to minimize the amount of time required for completion of recognition and avoid attempts to recognize outdated barcodes.

We can specify target barcode type and read barcode from an image by following the steps given below:

  1. Firstly, load an image using the BarCodeReader class.
  2. Next, set a barcode decode type, such as Code39Standard.
  3. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode of a specific type from an image using C#.

Recognize Barcode of a Specific Type from Image

Recognize Barcode of a Specific Type from Image in C#.

Read Barcode of Multiple Types from Image in C#

We can also specify multiple barcode types by following the steps given below:

  1. Firstly, load an image using the BarCodeReader class.
  2. Next, set a barcode decode types using the SetBarCodeReadType() method.
  3. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read barcodes of multiple types from an image using C#.

Read Barcodes of Multiple Types from Image in C#

Read Barcodes of Multiple Types from Image in C#

We can also specify multiple decode types in the constructor of the BarCodeReader class as shown below:

BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended);

Read Predefined Set of Barcode Types from Image in C#

We can read a predefined set of symbologies for recognition defined in the DecodeTypes class. We can set any of the following predefined set:

  • AllSupportedTypes - all supported barcode types
  • Types1D - all supported 1D symbologies
  • Types2D - all supported 2D symbologies
  • PostalTypes - all supported postal symbologies that are mainly used by postal services
  • MostCommonTypes - a set of most widely used barcode standards defined

We can specify a predefined set by following the steps given below:

  1. Firstly, Firstly, load an image using the BarCodeReader class.
  2. Next, set a barcode decode type in the BarCodeReader constructor or the SetBarCodeReadType method, such as DecodeType.Types2D.
  3. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode using predefined set of symbologies in C#.

Read Predefined Set of Barcode Types from Image in C#

Read Predefined Set of Barcode Types from Image in C#

Read Multiple Barcodes from Image in C#

We can also read all the available barcodes from an image by following the steps given below:

  1. Firstly, load an image using the BarCodeReader class.
  2. Next, set a barcode decode type as ALL_SUPPORTED_TYPES.
  3. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read multiple barcodes from an image using C#.

Read Multiple Barcodes from Image.

Read multiple barcodes from an image.

Get X and Y Coordinates of Barcode using C#

We can read the X and Y coordinates of the detected barcode from an image by following the steps given below:

  1. Firstly, load an image using the BarCodeReader class.
  2. Next, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  3. Then, loop through the results and check if the Region is not null.
  4. After that, get the Points array bounding the barcode region using the Points.
  5. Finally, show the barcode’s X and Y points.

The following code sample shows how to get the X and Y coordinate points of a barcode from an image using C#.

Get X and Y Coordinates of Barcode

Get X and Y coordinates of a barcode from an image.

Read Barcode From Specific Region of Image in C#

We can read a barcode from a specific region or an area of an image by following the steps given below:

  1. Firstly, read buffer data of an image file using the Bitmap method.
  2. Next, create an instance of the BarCodeReader class. It takes an image, a rectangle area, and a Decode Type as arguments.
  3. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  4. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from a specific region of an image using C#.

Read Barcode From Specific Region of Image.

Read barcodes from a specific region of an image in C#.

Read Barcode From Multiple Regions of Image in C#

We can also read barcode from multiple regions of an image by following the steps given below:

  1. Firstly, load an image using the Bitmap class.
  2. Next, define rectangles using the Rectangle class.
  3. Then, create an instance of the BarCodeReader class.
  4. Meanwhile, call the SetBarCodeImage() with an image object, and rectangles as arguments.
  5. Optionally, set barcode read types using the SetBarCodeReadType() method.
  6. After that, get recognition results in BarCodeResult class object using the ReadBarCodes() method.
  7. Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from multiple regions of an image using C#.

Read Barcode From Multiple Regions of Image.

Read barcodes from Multiple region of an image in C#.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • Load barcode image file;
  • Read images as bitmap or using file stream;
  • recognize barcodes of different types from an image;
  • read barcode coordinates;
  • detect and read multiple barcodes from a single image;
  • detect and read barcodes from a specific area of an image.

Besides reading barcode from an image in C#, you can learn more about Aspose.BarCode for .NET API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also