C# Barcode Reader Library
How to scan, read barcode image from PDF, Tiff, Office Word file in C#.NET


Visual C# Codes for Barcode Reading Using .NET Imaging SDK Library













Apply Barcode Scanning Options


In your C# code, you can use class "ReaderSettings" to apply the barcode read options. The class is used to configure the barcode reader.


  • Types : List of barcode formats to be scanned on the barcode file. You can add multiple barcode formats by using the method ReaderSettings.AddTypesToRead()
                ReaderSettings setting = new ReaderSettings();
                setting.AddTypesToRead(BarcodeType.Code39);
                setting.AddTypesToRead(BarcodeType.Code128);
    

  • Direction: Type: ScanDirection. Gets and sets the direction of reading the source image.
    Enum: ScanDirection
    • Undefined: Scan the source in different directions and stop once the 1st barcode is found.
    • LeftToRight: Scan the source from left to right only.
    • TopToBottom: Scan the source from top to bottom only
    • RightToLeft: Scan the source from right to left only.
    • BottomToTop: Scan the source from bottom to top only.
    • Horizontal: Scan the source in horizontal.
      Equivalent to: LeftToRight | RightToLeft
    • Vertical: Scan the source in vertical.
      Equivalent to: TopToBottom | BottomToTop
    • All: Scan the source in all 4 directions.

  • RegionOfInterest: Specifies the region on the source image of which it would scan.
    Unit: in percent
    Remarks: X, Y, (X + Width) and (Y + Height) of the Rectangle must be in the range of 0 to 100; otherwise, the region is the whole image.

  • NumberOfBarcodeToRead: Limit the number of barcode that read from the source.
    MoreThanOne: read all barcodes.







Scanned Barcode Information


Once you have scanned the image file or PDF, TIFF document, and you will get a list of Barcode object. Each "Barcode" object contains scanned barcode information.


  • Type: Get the format of the scanned barcode.
  • RawData: Gets barcode message data in byte array
  • DataString: Gets barcode message data in string.
  • BeginLine: Gets the begin line segment of the barcode, which usually is the left edge of the barcode boundary.
  • EndLine: Gets the end line segment of the barcode, which usually is the right edge of the barcode boundary.
  • BoundingRectangle: Gets the minimum bounding rectangle of the barcode.
  • Direction: Gets the direction of the barcode.
    LeftToRight: from left to right
    TopToBottom: from top to bottom
    RightToLeft: from right to left
    BottomToTop: from bottom to top






How to reduce the barcode scanning time using C# barcode reader library?


One effective method to reduce the barcode scanning time, is to scan partial area of the large image. XImage.Barcode Reader library includes one important feature to quickly scan and find a barcode from the image with large size.

If you have large number of scanned invoice documents, and all of them are in high resolution. If the barcodes inside the invoice documents are in the same location, for example, bottom of the document. The following barcode scan settings will reduce the barcode reading time effectively.

  • Set the barcode format to scan
  • Set the property NumberOfBarcodeToRead to One
  • Set the property Direction to BottomToTop
  • Now the barcode scanner will read and scan the barcode image from bottom to up, and once it finds one barcode, it will stop scanning.

            ReaderSettings setting = new ReaderSettings();
            setting.AddTypesToRead(BarcodeType.Code128);
            setting.NumberOfBarcodeToRead = NumberOfBarcodeToRead.One;
            setting.Direction = ScanDirection.BottomToTop;

            Barcode[] barcodes = BarcodeReader.ReadBarcodes(
                setting, new Bitmap("C:/Test-Files/scan-max-one-barcode.png"));

            foreach (Barcode aBarcode in barcodes)
            {
                Debug.WriteLine(aBarcode.Type + ": " + aBarcode.DataString);
            }






How to read multiple barcodes with different barcode formats using C#?


It is really easy to scan and detect multiple barcodes with different formats from an image using C# barcode reader library.

  • Use ReaderSettings.AddTypesToRead() method to add scanning barcode formats. The C# code below it will scan all Code 39 and Code 128 barcodes from the barcode image.
            ReaderSettings setting = new ReaderSettings();
            setting.AddTypesToRead(BarcodeType.Code39);
            setting.AddTypesToRead(BarcodeType.Code128);

            Barcode[] barcodes = BarcodeReader.ReadBarcodes(
                setting, new Bitmap("C:/Test-Files/multi-barcode-format.png"));

            foreach (Barcode aBarcode in barcodes)
            {
                Debug.WriteLine(aBarcode.Type + ": " + aBarcode.DataString);
            }



You will get the following output
Code39: ABC-123W
Code128: CODE 128







How to scan barcode from target region to faster reading using C#?


Scanning barcode from the specified region is an important feature by XImage.BarcodeReader library. It will not only improve the recognition speed, but also improve the recognition rate.

You can specify the target region in the image by using a System.Drawing.Rectangle object.

Now we will show you how to scan barcode from the top region of the image file in C# code.

  • Set a target scan region in a Rectangle object to ReaderSettings.RegionOfInterest property

            ReaderSettings setting = new ReaderSettings();
            setting.AddTypesToRead(BarcodeType.Code39);

            setting.RegionOfInterest = new Rectangle(0, 0, 100, 50);

            Barcode[] barcodes = BarcodeReader.ReadBarcodes(
                setting, new Bitmap("C:/Test-Files/scan-target-region.JPG"));

            foreach (Barcode aBarcode in barcodes)
            {
                Debug.WriteLine(aBarcode.Type + ": " + aBarcode.DataString);
            }






How to read GS1 data elements from GS1 barcodes using C#?


The GS1 system originated in the United States and was established in 1973 by the Uniform Product Code Council, known until recently as the Uniform Code Council, Inc. (UCC) and since 2005 as GS1 US.

A GS1 message contains a group GS1 elements. Each GS1 element string is the combination of a GS1 Application Identifier (AI Code) and a GS1 Application Identifier data field (AI Data). The AI Code is a two to four numeric digits.

The GS1 System supports the following barcode formats to encode GS1 data message:

  • QR Code
  • Data Matrix
  • EAN/UPC
  • GS1-128
  • GS1 DataBar
  • ITF-14


  • GS1-128 is part of Code 128. You need choose barcode format as BarcodeType.Code128 to decode GS1-128 barcodes.
  • Call BarcodeReader.ReadGS1Barcodes() to extract the GS1 data message from GS1-128 barcode images.

            ReaderSettings setting = new ReaderSettings();
            setting.AddTypesToRead(BarcodeType.Code128);

            string[] barcodes = BarcodeReader.ReadGS1Barcodes(setting, new Bitmap("C:/Test-Files/gs1-128-demo.png"));

            foreach (string aBarcode in barcodes)
            {
                Debug.WriteLine("GS1-128: " + aBarcode);
            }




Here we will get the following scanned data message
GS1-128: (01)99012345678900(400)PO20240516-123(10)ABC123(11)240516







How to read Unicode text from 2d barcodes using C#?


Most of the 2d barcodes such as QR Code, Data Matrix, PDF-417 supports date encoding in byte array. And barcode generator software will store the Unicode characters into byte array using UTF8 encoding.

Here we will show how to read, decode Unicode characters from QR Code using C# XImage.BarcodeReader library.

  • Read string text stored in QRCode using method BarcodeReader.ReadBarcodes()
  • Convert each character to one byte data, and store in a byte array object
  • Using UTF8 encoding to convert byte array object to string. You will get the Unicode text

            ReaderSettings setting = new ReaderSettings();
            setting.AddTypesToRead(BarcodeType.QRCode);

            Barcode[] barcodes = BarcodeReader.ReadBarcodes(
                setting, new Bitmap("C:/Test-Files/scan-unicode.png"));

            foreach (Barcode aBarcode in barcodes)
            {
                List<byte> bytes = new List<byte>();
                foreach (char c in aBarcode.DataString)
                    bytes.Add((byte)c);

                Debug.WriteLine(aBarcode.Type + ": " + System.Text.Encoding.UTF8.GetString(bytes.ToArray()));
            }