C# Data Matrix Generator Data Encoding Tutorial
C# sample source code to input valid data, generate 2D Data Matrix images in C#.NET
Using Free C# source code to generate Data Matrix image labels for ASP.NET Web & Windows Applications
In this C# tutorial, you will learn how to generate Data Matrix barcode with various data format in .NET ASP.NET, Windows applications.
- Choose right Data Matrix data mode
- Create GS1 Data Matrix
- Encode international text in Data Matrix
How to create 2d Data Matrix barcodes using C#
C# Data Matrix Introduction
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 barcode generator is a mature, easy-to-use .NET barcode component, written in Visual C#. It is easy to integrate barcode component into C#.NET development environments, and allows developers to quickly and easily add barcode generation and recognition functionality to .NET applications using C# class.
This document provides a complete C# source code for encoding Data Matrix barcode images in C# class using C# Barcode generation .net SDK.
OnBarcode C# Barcode Generator is designed to generate, create Data Matrix and other linear & 2d barcodes in Microsoft Word.
Encoding Data Matrix using ASCII Mode with complete C# sample source code
ASCII encodation is the default set for the first symbol character in all symbol sizes. It encodes ASCII data,
double density numeric data and symbology control characters. Symbology control characters include function
characters, the pad character and the switches to other code sets.
Sample C# source code to encode Data Matrix data using ASCII Mode.
DataMatrix barcode = new DataMatrix();
// Data Matrix Barcode Basic Settings
barcode.Data = "112233445566";
barcode.DataMode = DataMatrixDataMode.ASCII;
// if your selected format mode doesnot have enough space to encode your data,
// the library will choose the right format mode for you automatically.
barcode.FormatMode = DataMatrixFormatMode.Format_16X48;
// Set the ProcessTilde property to true, if you want use the tilde character "~"
// to specify special characters in the input data. Default is false.
//
// 1) 1-byte character: ~0dd/~1dd/~2dd (character value from 000 ~ 255); ASCII character '~' is presented by ~126
// Strings from "~256" to "~299" are unused
// modified to FS, GS, RS and US respectively.
// 2) 2-byte character (Unicode): ~6ddddd (character value from 00000 ~ 65535)
// Strings from "~665536" to "~699999" are unused
// 3) for GS1 AI Code:
// ~ai2: AI with 2 digits
// ~ai3: AI with 3 digits
// ~ai4: AI with 4 digits
// ~ai5: AI with 5 digits
// ~ai6: AI with 6 digits
// ~ai7: AI with 7 digits
// 4) ECI: ~7dddddd (valid value of dddddd from 000000 to 999999)
// 5) ~rp: Reader Programming (for ASCII mode and Auto mode only)
// This should be located at the beginning of the encoding data, e.g. data = "~rpABCD1234".
// 6) ~m5: 05 Macro (for ASCII mode and Auto mode only)
// This should be located at the beginning of the encoding data, e.g. data = "~m5ABCD1234".
// 7) ~m6: 06 Macro (for ASCII mode and Auto mode only)
// This should be located at the beginning of the encoding data, e.g. data = "~m6ABCD1234".
barcode.ProcessTilde = true;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 3;
barcode.LeftMargin = 0;
barcode.RightMargin = 0;
barcode.TopMargin = 0;
barcode.BottomMargin = 0;
barcode.Resolution = 96;
barcode.Rotate = Rotate.Rotate0;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-datamatrix.png");
Encoding Data Matrix using C40 Mode with complete C# sample source code
The C40 mode is designed to optimise the encoding of upper-case alphabetic and numeric
characters but also enables other characters to be encoded by the use of shift characters in conjunction with
the data character.
Sample C# source code to encode Data Matrix data using C40 Mode.
DataMatrix barcode = new DataMatrix();
// C40 encodation is designed to optimise the encoding of uppercase and numeric characters;
// but also enables to encoded all other ASCII characters.
barcode.Data = "ABC123456XYZ";
barcode.DataMode = DataMatrixDataMode.C40;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_16X16;
// 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-c40.png");
Encoding Data Matrix using Text Mode with complete C# sample source code
Text mode is designed to encode normal printed text, which is predominantly lowercase characters. It is
similar in structure to the C40 encodation set, except that lowercase alphabetic characters are directly
encoded (i.e. without using a shift). Upper-case alphabetic characters are preceded by a Shift 3.
Sample C# source code to encode Data Matrix data using Text Mode.
DataMatrix barcode = new DataMatrix();
// Text encodation is designed to optimise the encoding of lowercase and numeric characters;
// but also enables to encoded all other ASCII characters.
barcode.Data = "abc123456xyz";
barcode.DataMode = DataMatrixDataMode.Text;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_16X16;
// 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-text.png");
Encoding Data Matrix using ANSI X12 Mode with complete C# sample source code
ANSI X12 encodation is used to encode the standard ANSI X12 electronic data interchange characters, which
are compacted three data characters to two codewords in a manner similar to C40 encodation. It encodes
upper-case alphabetic characters, numerics, space and the three standard ANSI X12 terminator and
separator characters.
Sample C# source code to encode Data Matrix data using ANSI X12 Mode.
DataMatrix barcode = new DataMatrix();
// X12 encodation is only used for the standard ANSI X12 electronic data interchange characters.
// Valid characters: 0 ~ 9, A ~ Z, space and X12 terminator (<CR>), sepertors ('*', '>')
// Use "~013" to represent <CR> and ProcessTilde must be enable.
barcode.Data = "ABC*123~013";
barcode.ProcessTilde = true;
barcode.DataMode = DataMatrixDataMode.X12;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_16X16;
// 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-x12.png");
Encoding Data Matrix using EDIFACT Mode with complete C# sample source code
The EDIFACT encodation scheme includes 63 ASCII values (values from 32 to 94) plus an Unlatch character
(binary 011111) to return to ASCII encodation. EDIFACT encodation encodes four data characters in three
codewords. It includes all the numeric, alphabetic and punctuation characters defined in the EDIFACT Level A
character set without any of the shifts required in C40 encodation.
Sample C# source code to encode Data Matrix data using EDIFACT Mode.
DataMatrix barcode = new DataMatrix();
// EDIFACT encodation is only used for 63 ASCII values (from 32 to 94).
// Valid characters: 0 ~ 9, A ~ Z, and most punctuations (excluding '_', '`' and '~').
barcode.Data = "ABC123&XYZ (S)";
barcode.DataMode = DataMatrixDataMode.Edifact;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_16X16;
// 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-edifact.png");
Encoding Data Matrix using Base 256 Mode with complete C# sample source code
The Base 256 encodation scheme shall be used to encode any 8-bit byte data, including extended channel
interpretations and binary data.
Sample C# source code to encode Data Matrix data using Base 256 Mode.
DataMatrix barcode = new DataMatrix();
// Base256 encodation is used to encode any 8-bit byte data.
// Each byte should be represented in format "~ddd", which "ddd" is a decimal number between 0 to 255.
// ProcessTilde must be enable to support this feature.
barcode.Data = "~000~001~003~253~254~255";
barcode.ProcessTilde = true;
barcode.DataMode = DataMatrixDataMode.Base256;
// Select format mode
barcode.FormatMode = DataMatrixFormatMode.Format_16X16;
// 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-base256.png");
Generate GS1 DataMatrix barcode images using C#
The foundational GS1 standard that defines how identification keys, data attributes and barcodes must be used in business applications.
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.
The GS1 system has adopted GS1 DataMatrix partly because, like GS1 QR Code, GS1 DataMatrix can encode GS1 system data structures and offers other technical advantages. Its
compact design and the existence of various production methods that accommodate placing the symbology onto various substrates offer certain advantages over other symbologies currently in the GS1 system.
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, such as Thai
Sample C# source code to encode Thai text in Data Matrix barcode
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");