C# Data Matrix Reader Library
How to read, scan 2D Data Matrix barcode images using C#.NET


Data Matrix barcode reading from documents and image files Using C#. Support read barcode from PDF, Tiff, Jpeg, Word files.





In this C# tutorial, you will learn how to read, recognize Data Matrix 2d barcode from PDF, Tiff and image files in your C# asp.net, Windows apps.

  • Easy to read, scan Data Matrix barcodes from image, PDF files
  • Reading Data Matrix from PDF, Tiff and PNG, JPG, Bitmap image files
  • Advanced scanning options to speed up reading

How to scan, read Data Matrix from PDF, Tiff, image files using C#

  1. Download XDoc.Barcode Reader C# library
  2. Install C# library to read Data Matrix from files
  3. Step by Step Tutorial










  • Decode Data Matrix barcode from documents file like PDF, Word, Excel and PowerPoint using C#
  • Recognize Data Matrix from scanned raster image file, like png, gif & bmp using C#
  • Read all Data Matrix barcodes from target document image file quickly in C#.NET
  • Scan Data Matrix barcode from whole page or any defined rectangular area using C#
  • Free to set the number of Data Matrix needed to be recognized from source file
  • Return & output scanned Data Matrix values to data string in C#.NET class application
  • Support other linear & 2d barcode recognition from document image file using C# code






About Data Matrix barcode


Data Matrix is a commonly-used 2D barcode type. It consists of black & white modules which are grouped in a square or rectangular pattern. Similar to other 2d barcodes, Data Matrix barcode can store huge information with a small printout barcode size. Besides, Data Matrix also owns strong error correction levels, which makes Data Matrix barcode readable even when the barcode damage has been up to 60%.

There are many factors that affect the recognition & reading of Data Matrix barcode from document image file in .NET class application, like quiet zone width of Data Matrix and resolution of generated Data Matrix barcode image. Besides barcode reading functions, RasterEdge DocImage SDK for .NET also supports fast Data Matrix barcode generation in VB.NET class application.







Sample C# source code for Data Matrix barcode reading from PDF document


If you want to read & recognize Data Matrix barcode from scanned PDF document file in C#.NET class application, this barcode reader control will be a great choice. With one line of code, you can detect and decode all the Data Matrix barcodes existing on the target PDF document file. Besides, with the application of X & Y location, you are allowed to define the Data Matrix barcode scanning area.



        public static void ReadDataMatrixBarcodeFromPDF()
        {
            String inputFilePath = @"C:\1.pdf";

            //  Open PDF file
            PDFDocument doc = new PDFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






Sample C# source code for reading Data Matrix from TIFF image file


With RasterEdge .NET Barcode Reader SDK, users are allowed to reading Date Matrix not only from png, jpeg, bmp and gif image files, but also tiff images. This is the C# demo code for reading Date Matrix from tiff.



        public static void ReadDataMatrixBarcodeFromTiff()
        {
            String inputFilePath = @"C:\1.tif";

            //  Open TIFF file
            TIFFDocument doc = new TIFFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






Sample C# source codes to decode Data Matrix from JPEG image file


This C#.NET barcode scanning control supports reading & decoding one or more 2D Data Matrix barcode(s) from several commonly-used raster image file formats, including png, jpeg, bmp, tiff and gif. Using the sample C# code below, you can easily detect & decode all Data Matrix barcodes from png image files in .NET class application.



        public static void ReadDataMatrixBarcodeFromJpeg()
        {
            String inputFilePath = @"C:\1.jpg";

            //  Open JPEG file
            Bitmap bitmap = new Bitmap(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, bitmap);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






C#: scan, read Data Matrix barcode data from stream objects using C#


Using barcode reader SDK, you can not only read 2d data matrix from image document files, you can also scan, read data matrix barcode data from stream objects directly using C#.



        public static void ReadDataMatrixBarcodeFromBitmapStream()
        {
            String inputFilePath = @"C:\1.png";

            using (FileStream fs = File.Open(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                //  Open PNG stream
                Bitmap bitmap = new Bitmap(fs);
                //  Initial reader's setting
                ReaderSettings settings = new ReaderSettings();
                //  Set target barcode type
                settings.AddTypesToRead(BarcodeType.DataMatrix);
                //  Scan barcodes from the document.
                Barcode[] result = BarcodeReader.ReadBarcodes(settings, bitmap);

                if (result.Length > 0)
                {
                    foreach (Barcode barcode in result)
                        Console.WriteLine("Barcode Data: " + barcode.DataString);
                }
                else
                    Console.WriteLine("[No Barcode Found]");
            }

        }






Sample C# source code to read, recognize Data Matrix barcodes from Microsoft Word document in .NET


Data Matrix barcode reading from Word works in the same way with that from PDF document. You can also detect and recognize all the Data Matrix barcodes from target Word page within a single line of code and return the decoded barcode values to data string. Similarly, if the Data Matrix barcode(s) needed to be decoded is located in a fixed area, you can use X & Y location to set the barcode scanning area using C# code.



        public static void ReadDataMatrixBarcodeFromWord()
        {
            String inputFilePath = @"C:\1.docx";

            //  Open .docx file
            DOCXDocument doc = new DOCXDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






More advanced features to read Data Matrix barcodes


To improve the Data Matrix reading speed, you can scan and read Data Matrix data from image region area, PDF, Tiff pages list. The following demo codes will show how to do it.







C#: scan, read Data Matrix barcodes with multiple barcode types using C#


The C# code below will scan, read multiple barcode types from an image file.



        public static void ReadDataMatrixBarcodeWithMultipleTypes()
        {
            String inputFilePath = @"C:\1.jpg";

            //  Open JPEG file
            Bitmap bitmap = new Bitmap(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set multiple target barcode types to Code128, DataMatrix and QRCode
            settings.AddTypesToRead(BarcodeType.Code128);
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            settings.AddTypesToRead(BarcodeType.QRCode);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, bitmap);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                {
                    Console.WriteLine("Barcode Type: " + barcode.Type.ToString());
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
                }
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }


The following c# source code will scan, read all sdk supported barcode types from the document.



        public static void ReadDataMatrixBarcodeWithAllTypesSupported()
        {
            String inputFilePath = @"C:\1.jpg";

            //  Open JPEG file
            Bitmap bitmap = new Bitmap(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode types to All.
            settings.AddTypesToRead(BarcodeType.All);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, bitmap);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                {
                    Console.WriteLine("Barcode Type: " + barcode.Type.ToString());
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
                }
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






C#: scan, read, decode 2d Data Matrix barcodes from a specified image region


The C# code below will scan, read Data Matrix barcode datas from an image region.



        public static void ReadDataMatrixBarcodeFromImageRegion()
        {
            String inputFilePath = @"C:\1.jpg";

            //  Open JPEG file
            Bitmap bitmap = new Bitmap(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);
            //  Set scan region: from 10% to 50% of width in horizontal (from left to right)
            //  and from 0% to 40% of height in vertical (from top to bottom).
            settings.RegionOfInterest = new Rectangle(10, 0, 50, 40);
            //  Scan barcodes from the document.
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, bitmap);

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }






C#: scan, read barcode Data Matrix from PDF page list


The C# code below will scan, read Data Matrix datas from Adobe PDF document pages list



        public static void ReadDataMatrixBarcodeFromPDFPagesList()
        {
            String inputFilePath = @"C:\1.pdf";

            //  Open PDF file
            PDFDocument doc = new PDFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);

            //  Scan barcodes from the first page.
            int pageIndex = 0;
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc.GetPage(pageIndex));

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");


        }






C#: scan, read barcode Data Matrix from tiff document pages list


The C# code below will scan, read Data Matrix datas from tiff document pages list



        public static void ReadDataMatrixBarcodeFromTiffPagesList()
        {
            String inputFilePath = @"C:\1.tif";

            //  Open TIFF file
            TIFFDocument doc = new TIFFDocument(inputFilePath);
            //  Initial reader's setting
            ReaderSettings settings = new ReaderSettings();
            //  Set target barcode type
            settings.AddTypesToRead(BarcodeType.DataMatrix);

            //  Scan barcodes from the first page.
            int pageIndex = 0;
            Barcode[] result = BarcodeReader.ReadBarcodes(settings, doc.GetPage(pageIndex));

            if (result.Length > 0)
            {
                foreach (Barcode barcode in result)
                    Console.WriteLine("Barcode Data: " + barcode.DataString);
            }
            else
                Console.WriteLine("[No Barcode Found]");

        }