C# Data Matrix Barcode Generator Library
Integration & Developer Guide for Data Matrix 2D barcode image generation using C#
"This .NET suite helps my team a lot, a bunch of time being saved. OnBarcode support is awesome, and we couldn't ask more than this."
- Bill Twain, Canada
Generate 2d barcode Data Matrix images in Visual C# .NET with complete sample C# source code
- Generate, create Data Matrix in Visual C# .NET applications
- Easy to install & integrate barcode Data Matrix generation library SDK into C# developments
- Generate over 30 linear, 2d barcode images in C#.NET including C# QR Code, C# PDF-417, C# Code 39, C# Code 128, C# GS1-128, C# EAN, C# UPC
- Generate Data Matrix images in C# class library
- Create barcode Data Matrix in C# ASP.NET web application
- Print Data Matrix 2d barcode in C# Windows Form project
- Draw 2d Data Matrix in SQL Server Reporting Services (SSRS) & Crystal Reports for .NET projects
- Easy to encode Data Matrix images to jpeg, gif, png, tiff, bitmap files in C# program
Data Matrix Barcode Introduction

Top
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.
Data Matrix Barcode Data Characters Encoding using C#

Top
Data Matrix Valid Encoding Character Set
Data Matrix barcode supports encoding
full ASCII characters and
entended 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 |
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
DataMatrix barcode = new DataMatrix();
// It could encode GS1 element(s) by inserting a FNC1 symbol before all data characters.
// Each element contains a GS1 prefix (in parentheses) and fixed (or variable) length data content.
barcode.Data = "(17)050101(10)ABC123";
// Set FNC1 to FNC1.FNC1_1ST_POS to enable GS1 compatible Data Matrix barcode generation
barcode.FNC1 = FNC1.FNC1_1ST_POS;
barcode.DataMode = DataMatrixDataMode.Auto;
// 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-gs1.png");
Generate Data Matrix barcode with non-English text (Thai) using C# in asp.net, winforms
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");
Extended Channel Interpretation (ECI) in Data Matrix using C#
In Data Matrix, you can change the encoding character set to others using ECI.
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");
Implement Structure Append in Data Matrix using C#
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#
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.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 barcode property settings in C#
Category |
Properties |
Value |
Comments |
Basic |
Property: Data
URL: DATA
|
Type: string
Default: "DataMatrix"
|
Barcode value to encode.
Data Matrix Valid Data Char Set:
- ASCII values 0 - 127 in accordance with the US national version of ISO/IEC 646
NOTE:
This version consists of the G0 set of ISO/IEC 646 and the C0 set of ISO/IEC 6429 with values 28 - 31
modified to FS, GS, RS and US respectively.
- ASCII values 128 - 255 in accordance with ISO 8859-1. These are referred to as extended ASCII.
|
|
Data Matrix Special |
Property: ProcessTilde
URL: PROCESS-TILDE
|
Type: bool
Default: true
|
Set the ProcessTilde property to true, if you want use the tilde character "~" to specify special characters in the input data. Default is true.
|
Property: DataMode
URL: DATA-MODE
|
Type: DataMatrixDataMode
Default: DataMatrixDataMode. ASCII (1)
|
Data Matrix data encoding mode.
Valid values are:
-
DataMatrixDataMode.Auto (0): Barcode library will decide the best data mode for you.
-
DataMatrixDataMode.ASCII (1): it is used to encode data that mainly contains ASCII characters (0-127). This is the default encoding format by Barcode Library.
-
DataMatrixDataMode.C40 (2): it is used to encode data that mainly contains numeric and upper case
characters.
-
DataMatrixDataMode.Text (3): it is used to encode data that mainly contains numeric and lower case
characters.
-
DataMatrixDataMode.X12 (4):it is used to encode the standard ANSI X12 electronic data interchange characters.
-
DataMatrixDataMode.Edifact (5): it is used to encode 63 ASCII values (values from 32 to 94) plus an Unlatch character (binary 011111).
-
DataMatrixDataMode.Base256 (6): it is used to encode 8 bit values.
|
Property: FormatMode
URL: FORMAT-MODE
|
Type: DataMatrixFormatMode
Default: DataMatrixFormatMode. Format_10X10 (0)
|
Default is DataMatrixFormatMode.Format_10X10 (0). Specifies the Data Matrix Format to use on that symbology.
Valid values see enum DataMatrixFormatMode.Format_*X*;
|
Property: FNC1
URL: FNC1
|
Type: FNC1
Default: FNC1. FNC1_NONE (0)
|
To encode GS1 compatible Data Matrix barcode, you need set FNC1 value to FNC1.FNC1_1ST_POS (1).
|
Property: StructuredAppend
URL: STRUCTURED-APPEND
|
Type: bool
Default: false
|
Set StructuredAppend property to true, then Structured Append is enabled. |
Property: SymbolCount
URL: SYMBOLE-COUNT
|
Type: int
Default: 0
|
Set SymbolCount property to the number of total symbols which make the sequence. |
Property: SymbolIndex
URL: SYMBOL-INDEX
|
Type: int
Default: 0
|
Set SymbolIndex property to the position of current symbol in the secuence (Start with 0). |
Property: FileId
URL: FILE-ID
|
Type: int
Default: 0
|
Set FileId property to be identified to the same file. |
|
Barcode Size Related |
Property: AutoResize
URL: AUTO-RESIZE
|
Type: bool
Default: false
|
Auto resize the generated barcode image.
|
Property: BarAlignment
URL: BAR-ALIGNMENT
|
Type: int
Default: 1 (center)
|
Barcode horizontal alignment inside the image. 0: left, 1: center, 2: right. |
Property: UOM
URL: UOM
|
Type: UnitOfMeasure
Default: PIXEL (0)
|
Unit of meature for all size related settings in the library.
Valid values:
- UnitOfMeasure.PIXEL (0)
- UnitOfMeasure.CM (1)
- UnitOfMeasure.INCH (2)
|
Property: X
URL: X
|
Type: float
Default: 3
|
barcode module width and height, default is 3 pixel. |
Property: BarcodeWidth
URL: BARCODE-WIDTH
|
Type: float
Default: 0
|
Barcode image width.
If BarcodeWidth setting is smaller than the barcode required minimum width, the library will automatically reset to barcode minimum width. |
Property: BarcodeHeight
URL: BARCODE-HEIGHT
|
Type: float
Default: 0
|
Barcode image height.
If BarcodeHeight setting is smaller than the barcode required minimum height, the library will automatically reset to barcode minimum height. |
Property: LeftMargin
URL: LEFT-MARGIN
|
Type: float
Default: 0
|
Barcode image left margin size. |
Property: RightMargin
URL: RIGHT-MARGIN
|
Type: float
Default: 0
|
Barcode image right margin size. |
Property: TopMargin
URL: TOP-MARGIN
|
Type: float
Default: 0
|
Barcode image top margin size. |
Property: BottomMargin
URL: BOTTOM-MARGIN
|
Type: float
Default: 0
|
Barcode image bottom margin size. |
Property: Resolution
URL: RESOLUTION
|
Type: int
Default: 72
|
Barcode image resolution in DPI (Dots per inch). |
Property: Rotate
URL: ROTATE
|
Type: Rotate
Default: Rotate.Rotate0 (0)
|
Valid values:
- Rotate.Rotate0 (0)
- Rotate.Rotate90 (1)
- Rotate.Rotate180 (2)
- Rotate.Rotate270 (3)
|
|
Barcode Colors |
Property: BackColor
URL: BACK-COLOR
|
Type: Color
Default: white
|
Barcode image background color. |
Property: ForeColor
URL: FORE-COLOR
|
Type: Color
Default: black
|
Barcode image foreground color.
|
|
In WebStream query string, please use int value for Enums, "true" and "false" for bool. |
Sample Code: How to generate Data Matrix Barcode using C#?
Creating Data Matrix barcode in C# class example:
using System;
using System.Collections.Generic;
using System.Text;
using OnBarcode.Barcode;
using System.Drawing.Imaging;
using System.Drawing;
DataMatrix datamatrix = new DataMatrix();
// Barcode data to encode
datamatrix.Data = "OnBarcode";
// Data Matrix data mode
datamatrix.DataMode = DataMatrixDataMode.ASCII;
// Data Matrix format mode
datamatrix.FormatMode = DataMatrixFormatMode.Format_16X16;
/*
* Barcode Image Related Settings
*/
// Unit of meature for all size related setting in the library.
datamatrix.UOM = UnitOfMeasure.PIXEL;
// Bar module size (X), default is 3 pixel;
datamatrix.X = 3;
// Barcode image left, right, top, bottom margins. Defaults are 0.
datamatrix.LeftMargin = 0;
datamatrix.RightMargin = 0;
datamatrix.TopMargin = 0;
datamatrix.BottomMargin = 0;
// Image resolution in dpi, default is 72 dpi.
datamatrix.Resolution = 72;
// Created barcode orientation.
//4 options are: facing left, facing right, facing bottom, and facing top
datamatrix.Rotate = Rotate.Rotate0;
// Generate data matrix and encode barcode to gif format
datamatrix.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif;
datamatrix.drawBarcode("C:\\datamatrix.gif");
/*
You can also call other drawing methods to generate barcodes
public void drawBarcode(Graphics graphics);
public void drawBarcode(string filename);
public Bitmap drawBarcode();
public void drawBarcode(Stream stream);
*/
More C# Barcode Generation Tutorials for Each Barcode

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