C# Data Matrix Barcode Generator Library
Integration & Developer Guide for Data Matrix 2D barcode image generation using C#
Generate 2d barcode Data Matrix images in Visual C# .NET with complete sample C# source code
In this C# tutorial, you will learn how to create Data Matrix 2d barcode images in ASP.NET MVC, Windows Forms applications using C#.
- Data Matrix encodes character set (full ASCII, Unicode, byte array) in image file, Stream and byte array object
- Data Matrix encodes GS1 data elements
- Store and split long data message into multiple Data Matrix barcode images
- Fully customizable with Data Matrix barcode image dimension width & height
- Generate and print Data Matrix barcode image in JPG, PNG, BMP, SVG, EPS raster and vector file formats
- Comprehensive C# options to customize Data Matrix with ECC 200 for ISO/IEC 16022
How to create Data Matrix barcodes in ASP.NET and .Windows Forms, WPF using C#
Data Matrix, also known as Data Matrix ECC200, is great 2-dimensional matrix barcode to store different data up to 2,335 alphanumeric characters.
C# Data Matrix Generator is one of the functions in OnBarcode's
.NET Barcode Generation Controls, which supports generating & printing Data Matrix and 20+ other linear & 2D bar codes for C# applications.
OnBarcode C# Barcode Generator makes it easy to generate, create Data Matrix and other linear & 2d barcodes in Microsoft Word.
This document is providing a detailed C# source code about generating Data Matrix barcodes in C# class using
C# Barcode generation component. Complete Data Matrix custmoization settings is included in
C# Data Matrix generation guide.
Quick to create Data Matrix using C#

Top
Using C# Barcode Library, you can easily generate 2d Data Matrix barcode images in your various C#.NET projects, such as ASP.NET Core, MVC web and WPF, WinForms desktop apps.
The C# codes below show how to quickly generate a Data Matrix barcode, and save it into a
PNG image file, store it in a
Stream object in memory,
and store data on
byte array.
using OnBarcode.Barcode;
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
// create data matrix barcode on a png image file
barcode.drawBarcode("C://Output//data-matrix-sample.png");
// store barcode on a Stream object
Stream barcodeInStream = new MemoryStream();
barcode.drawBarcode(barcodeInStream);
// get barcode in byte array
byte[] barcodeInBytes = barcode.drawBarcodeAsBytes();
Data Matrix Barcode Data Characters Encoding using C#

Top
Data Matrix Valid Encoding Character Set
Data Matrix barcode supports encoding
full ASCII characters and
extended ASCII characters.
- Full ASCII (all 128 characters)
- Extended ASCII (ISO 8859-1 values 128-255)
- Other character sets (e.g. Arabic, Cyrillic, Greek, Hebrew) may be supported when using the Extended Channel Interpretation (ECI) protocol
Data Matrix Barcode Maximum Data Length Size
Depends on your Data Matrix encoding data mode, Data Matrix supports maximum length from 1,555 to 3,116 characters.
- Alphanumeric data: up to 2,335 characters
- Byte data (8-bit): 1,555 characters
- Numeric data: 3,116 digits
Data matrix Encoding Data Mode
Data Matrix will encode the data using any combination of six encoding modes (schemes).
OnBarcode C# Barcode Generator library will automatically choose the right data modes for you using the following C# codes.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.DataMode = DataMatrixDataMode.Auto;
ASCII |
double digit numerics |
DataMatrixDataMode.ASCII |
ASCII values 0 - 127 |
Extended ASCII values 128 - 255 |
C40 |
Upper-case alphanumeric |
DataMatrixDataMode.C40 |
Lower case and special characters |
Text |
Lower-case alphanumeric |
DataMatrixDataMode.Text |
Upper case and special characters |
X12 |
ANSI X12 EDI data set |
DataMatrixDataMode.X12 |
EDIFACT |
ASCII values 32 - 94 |
DataMatrixDataMode.Edifact |
Base 256 |
All byte values 0 - 255 |
DataMatrixDataMode.Base256 |
Advanced data encoding in Data Matrix using C#
Here we will show more complex data encoding in Data Matrix generation using C#. It will instruct how to encode non-printable characters, Unicode text, binary data, GS1 data elements in Data Matrix in C#.
Encode non-printable chars in Data Matrix
Some ASCII characters are non printable ones, and you cannot express keyboard to input those chars directly in your C# program.
Here we will show you how to encode these non-printable chars (char 'carrage return' in the example) in Data Matrix in your C# code.
- All non-printable ASCII chars will be replaced with '~' plus its ASCII value in three digits. For example, char 'carrage return' will be replaced using '~013'.
- Set ProcessTilde to true
- Set DataMode to DataMatrixDataMode.Base256
DataMatrix barcode = new DataMatrix();
// encode non printable char '[CR]', between 'Data' and 'Matrix'
barcode.Data = "Data~013Matrix";
barcode.ProcessTilde = true;
barcode.DataMode = DataMatrixDataMode.Base256;
barcode.drawBarcode("C://Output//csharp-datamatrix-none-printable-chars.png");
When you scan and read Data Matrix image with non-printable ASCII chars encoded, you need process scanned chars also. View details here:
How to scan, read barcode with non-printable chars using C#?
Encode Unicode text in Data Matrix
Data Matrix barcode does not support international text by default. If you need encode Arabic, Greek, Thai text. There are two solutions for you.
- Convert internation text to byte array using UTF-8 encode, and generate Data Matrix using byte mode (Base 256)
- Use ECI to encode international text in Data Matrix
Sample C# source code to encode Thai text in Data Matrix barcode using Base 256 mode.
DataMatrix barcode = new DataMatrix();
// It may encode any Unicode characters after converting them to bytes in UTF-8 encode.
// And then, use Base256 encodation to encode these byte data.
String message = "สวัสดี";
byte[] bytes = Encoding.UTF8.GetBytes(message);
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
sb.Append("~" + b.ToString().PadLeft(3, '0'));
barcode.Data = sb.ToString();
barcode.ProcessTilde = true;
barcode.DataMode = DataMatrixDataMode.Base256;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_20X20;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-datamatrix-mode-thai.png");
Encode binary data in Data Matrix
Data Matrix symbol supports binary data encoding. To do it, you need apply the following settings
- Convert each byte data into string with '~' in the beginning
- Append all converted byte string into one single string data, and set it to the property Data
- Set ProcessTilde to true
- Set DataMode to DataMatrixDataMode.Base256
DataMatrix barcode = new DataMatrix();
String message = "สวัสดี";
byte[] bytes = Encoding.UTF8.GetBytes(message);
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
sb.Append("~" + b.ToString().PadLeft(3, '0'));
barcode.Data = sb.ToString();
barcode.ProcessTilde = true;
barcode.DataMode = DataMatrixDataMode.Base256;
barcode.drawBarcode("C://Output//csharp-datamatrix-binary.png");
Encode data through ECI (Extended Channel Interpretation) in Data Matrix
In Data Matrix, you can change the encoding character set to others using ECI (Extended Channel Interpretation).
In the following C# sample code, we will encode Thai text using ECI in Data Matrix barcode.
Key Settings:
- ProcessTilde: it should be enabled, and set to true.
- ECI value for Thai: Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes. In OnBarcode ECI solution, the value should be "~7000013".
- Each Thai text char value fromISO/IEC 8859-11: Eg. ก - 0xA1 - 161
DataMatrix barcode = new DataMatrix();
barcode.DataMode = DataMatrixDataMode.Auto;
// Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes.
String eci000013 = "~7000013";
// ISO/IEC 8859-11 Latin/Thai alphabet
// Byte values from 0xA0 to 0xFF are used for Thai characters.
// Eg. ก - 0xA1 - 161
String bytesInISO8859_11 = "~161";
// Set flag to enable '~' in data message.
barcode.ProcessTilde = true;
barcode.Data = eci000013 + bytesInISO8859_11;
// Barcode size related settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode(@"C:\DataMatrix_ECI000013.png");
Generate GS1 DataMatrix barcode images using C#
GS1 DataMatrix is a standalone, two-dimensional matrix symbology that is made up of square modules arranged within a perimeter finder pattern.
GS1 DataMatrix has been used in the public domain since 1994.
Data Matrix ISO version ECC 200 is the only version that supports GS1 system data structures, including Function 1 Symbol Character. The ECC 200 version of Data Matrix uses Reed-Solomon error correction, and this feature helps correct for partially damaged symbols.
Sample C# source code to encode GS1 Data Matrix barcode
Data Matrix Structure Append provides a mechanism for the data in a file to be split into blocks and be represented in more than one Data Matrix symbol (barcode image).
Each Data Matrix Structure Append symbol shall contain additional control information to enable the original data file to be properly reconstructed,
irrespective of the sequence in which the individual Data Matrix symbols are scanned and decoded.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix Structure Append";
barcode.DataMode = DataMatrixDataMode.Auto;
// Data Matrix structure append provides a mechanism to split a large data file into segaments and one Data Matrix symbol (image) for each segament.
// Enable Data Matrix structure append and set additional properties.
barcode.StructuredAppend = true;
barcode.FileId = 3; // File ID is 3.
barcode.SymbolCount = 10; // Total 10 segements for the file.
barcode.SymbolIndex = 2; // This symbol is Segment 2. The first Segment Index is 0
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_20X20;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("W://Projects//Test-Output//OnBarcode.com//csharp-datamatrix-structure-append.png");
Data Matrix Barcode Dimension Size in C#
Quick to create data matrix with specified width in C#
- Create a new DataMatrix object with data encoding
- Set property BarcodeWidth to 900 pixels. You will get a data matrix image with 900 pixels in width and 900 pixels in height.s
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.BarcodeWidth = 900; // 900 pixels or points
barcode.drawBarcode("C://Output//csharp-datamatrix-size.png");
Advanced Data Matrix dimension width & height settings in C#
The width of a Data Matrix barcode, including quiet zones, can be calculated from the following expression:
W = Row * X + 2Q
where
Note: if the Data Matrix is a square, the width and height are the same value.
In C# data matrix barcode generator, you can use the following barcode dimension size settings:
- UOM: Unit of measure. You can choose PIXEL, CM or INCH.
- X: Width of the bar module. The mimumum bar width is defined by the application specification
- FormatMode: Data Matrix symbol size format mode.
- LeftMargin, RightMargin, TopMargin, BottomMargin: Quiet zone. the minimum width of quiet zone is X.
C# source code to set Data Matrix barcode image size.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.DataMode = DataMatrixDataMode.Auto;
// Select symbol size format mode
barcode.FormatMode = DataMatrixFormatMode.Format_20X20;
// Barcode Size Related Settings
barcode.AutoResize = false;
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 5;
barcode.RightMargin = 5;
barcode.TopMargin = 5;
barcode.BottomMargin = 5;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("W://Projects//csharp-data-matrix-symbol-size.png");
Each symbol size format mode has
its' maxium data capacity.
If your encoding data exceeds its maximum data capacity, the barcode
generator library will choose the best data format mode for you.
Data Matrix Color and Image Option Settings using C#
Colors in Data Matrix
By default the Data Matrix modules color should be dark and the background color should be light. Sometimes the Data Matrix symbols will
also be printed to appear as light on dark.
The left image below is a standard Data Matrix with dark on light. And the right one is also a valid Data Matrix with light on dark.
The following C# source code will generate 3 Data Matrix images with the same encoding data, and different bar, space colors.
- A standard Data Matrix: bar color is black, and space color is white.
- A standard Data Matrix also: bar color is white, and space color is black.
- A Data Matrix: bar color is red, and space color is white. It is not a standard Data Matrix, you need make sure the barcode scanners support it.
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.drawBarcode("C://Output//csharp-datamatrix-dark-on-light.png");
}
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.ForeColor = Color.White;
barcode.BackColor = Color.Black;
barcode.drawBarcode("C://Output//csharp-datamatrix-light-on-dkar.png");
}
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.ForeColor = Color.Red;
barcode.drawBarcode("C://Output//csharp-datamatrix-colors.png");
}
You can also find more about color settings in barcode, such as transparent background barcodes here:
How to customize barcode colors using C# barcode library?
Image Settings in Data Matrix
To create Data Matrix in raster image format, you can specify the image format through property
System.Drawing.Imaging.ImageFormat.
And you can also directly output Data Matrix barcode into vector image format (SVG and EPS) through drawing method
drawBarcode2SVG() and
drawBarcode2EPS().
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.ImageFormat = ImageFormat.Jpeg;
barcode.drawBarcode("C://Output//csharp-datamatrix-image-jpeg.jpg");
}
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.drawBarcode2SVG("C://Output//csharp-datamatrix-image-svg.svg");
}
{
DataMatrix barcode = new DataMatrix();
barcode.Data = "A1B2C3D4E5F6G7H8I9J0K1L2";
barcode.drawBarcode2EPS("C://Output//csharp-datamatrix-image-eps.eps");
}
You can also get more image settings such as high resolution barcodes here:
How to customize barcode images using C# barcode generator library?
Data Matrix barcode property settings in C#
.NET Barcode Generator library has provided 20+ property settings to customize the generated Data Matrix barcode images.
You view
the complete list of Data Matrix barcode property settings here.
Sample Code: How to generate Data Matrix Barcode using C#?
More C# Barcode Generation Tutorials for Each Barcode

Top
Barcode Control for C#.NET - Bar Code Type Generation