C# Data Matrix Generator Library SDK
Integration & Developer Guide for Data Matrix 2D barcode image generation in 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
C# Data Matrix Generator 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. Here are some detailed tutorials for C# Data Matrix generation and data encoding, size & image setting.
Data Matrix Generator library, SDK & application
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 valid data sets
Data Matrix supports the following data sets encoding:
- All 128 characters of ASCII.
- Extended ASCII (ISO 8859-1 values 128-255)
Encode, 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
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");
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 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. |
Create Data Matrix Barcodes in C#

Top
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