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 supported character set (full ASCII, Unicode, byte array)
- Data Matrix encode GS1 format data message
- Encode long text message into multiple Data Matrix barcode images
- Data Matrix barcode image dimension size
- Generate and print Data Matrix barcode image in JPG, PNG, BMP, SVG, EPS formats
- Comprehensive C# property settings for ISO/IEC 16022
How to create Data Matrix barcodes in ASP.NET and .Windows Forms, WPF using C#
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
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 |
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#
.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