Home > .NET Barcode > C# Barcode Generation Guide > C# Data Matrix Generator Component for .NET


Data Matrix Generator Library for .NET in C# Class

How to Generate 2D Data Matrix Barcodes in .NET with C# Programming


  • 100% built in Visual C#, compatible with .NET Framwork 2.0, 3.0, 3.5, 4.0 and later
  • Run perfectly in many .NET Framework applications, like WinForms and ASP.NET
  • Create high-quality Data Matrix barcode images with C# codes in .NET
  • Insert & draw Data Matrix barcodes in Crystal Reports and RDLC Reports
  • Save generated Data Matrix barcodes as image files without using barcode fonts
  • Suitable for other linear & 2D barcodes generation, like EAN13, Code 128, QR Code, etc

C# 2D Data Matrix Barcode Introduction

Data Matrix is a 2 dimensional (2D) matrix barcode symbology, with high-density data and strong error correction ability. It has two types-ECC 200 and ECC000-140. In the real daily life, ECC200 is more widely used.




Quick to Create Data Matrix 2D Barcodes using C#

  • Create a new DataMatrix object
  • Enter Data Matrix encoding text in property Data
  • Use method drawBarcode() to generate, print Data Matrix barcode to a PNG image file
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-sample-image.png");





Data Matrix Text Encoding using C#

Data Matrix 2d barcode supports the following character set, data encoding using C#.
  • Full 128 ASCII table chars
  • Unicode text
  • GS1 Data Matrix


Encode Data Matrix ASCII characters

Full 128 ASCII (American Standard Code for Information Interchange) table includes 95 printable characters (such as Uppercase, Lowercase letters, digits), and 33 control characters (such as char 'carriage return'), which are not printable.

Creating Data Matrix with printable ASCII chars encoded is really easy. You can directly enter the ASCII chars to property Data.
DataMatrix barcode = new DataMatrix();
barcode.Data = "ABC-123-abc-*%$#";
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-ascii-chars.png");





To create Data Matrix with ASCII control chars (non-printing chars), you need convert each control char to it's ASCII interger value in three digits string, then pass to the property Data provide control char's ASCII integer value to property Data
  • Enable property ProcessTilde to true. The C# barcode library will process all 3-digit string following tilde "~".
  • In property Data, all control chars will be converted to it's ASCII integer value in 3-digit string behind char ~, and they will be inserted to the Data Matrix encoding code
DataMatrix barcode = new DataMatrix();
barcode.ProcessTilde = true;
barcode.Data = "ABC-1~0132-abc";
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-ascii-control-chars.png");



Encode Data Matrix Unicode Text String

  • Set property IsUnicodeData to true. The C# Data Matrix Generator library will automatically encode the Unicode text in the property Data to Data Matrix barcode in C# application.
DataMatrix barcode = new DataMatrix();
barcode.Data = "你好"; // hello in Chinese
barcode.IsUnicodeData = true;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-unicode-text.png");



Encode GS1 Data Matrix Barcodes

  • Set property FNC1Mode to DataMatrixFNC1Mode.FirstPosition. The barcode generation library will encode and print GS1 Data Matrix in C# application.
  • Enter the GS1 text string to property Data
DataMatrix barcode = new DataMatrix();
barcode.Data = "(17)050101";
barcode.FNC1Mode = DataMatrixFNC1Mode.FirstPosition;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-gs1-data.png");





Print Data Matrix with specified size in C#

Data Matrix barcodes usually are in square shapes. To create square Data Matrix, you need only set the barcode image width in property ImageWidth. The C# source code below will generate Data Matrix with width in 250 pixels, and height in 250 pixels.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.ImageWidth = 250;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-size.png");



Print rectangle Data Matrix



To create rectangle Data Matrix with specified width and height in C#, you can try the following example codes.
  • Set the property Format to DataMatrixFormat.Format_16X48. There are total 6 Data Matrix formats are rectangular symbols.
  • Set the property ImageWidth and ImageHeight to the specified size.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.Format = DataMatrixFormat.Format_16X48;
barcode.ImageWidth = 900;
barcode.ImageHeight = 300;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-size-rectangle.png");





Create Data Matrix with advanced settings using C#



Data Matrix Data Mode

Data Matrix will encode the text data using any combination of the following six encoding modes.
  • DataMatrixEncoding.ASCII. Encoding double digit numerics; ASCII values 0 - 127; Extended ASCII values 128 - 255.
  • DataMatrixEncoding.C40. Encoding Upper-case alphanumeric; Lower case and special characters.
  • DataMatrixEncoding.Text. Encoding Lower-case alphanumeric; Upper case and special characters.
  • DataMatrixEncoding.X12. Encoding ANSI X12 EDI data set.
  • DataMatrixEncoding.EDIFACT. Encoding ASCII values 32 - 94.
  • DataMatrixEncoding.Base256. Encoding All byte values 0 - 255.
Using BarcodeLib C# Data Matrix Barcode Generator library, we strongly recommend you to choose DataMatrixEncoding.Auto (default value) for property Encoding. With auto mode, the barcode library will automatically analyze the Data Matrix encoding text and select the suitable combination of the six encoding modes in C# application.

DataMatrix barcode = new DataMatrix();
barcode.Data = "https://www.barcodelib.com";
barcode.Encoding = DataMatrixEncoding.AUTO;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-data-mode.png");



Data Matrix Format Mode

Data Matrix ISO standard specifies 24 square symbols and 6 rectangular symbols available in ECC 200. Each symbol size includes different rows and columns with different data storage capacity.

Using BarcodeLib C# Barcode library, you can specify the Data Matrix format mode using property FormatMode. If your encoding Data Matrix data size is larger than your specified Data Matrix format mode, the barcode library will automatically choose the right format mode for you.

DataMatrix barcode = new DataMatrix();
barcode.Data = "https://www.barcodelib.com";
barcode.Format = DataMatrixFormat.Format_44X44;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-format-mode.png");



Data Matrix FNC1 character

When FNC1 appears in the first Data Matrix symbol character position, the Data Matrix barcode library will create GS1 Data Matrix in C# class.
  • Set property FNC1Mode with value DataMatrixFNC1Mode.FirstPosition
DataMatrix barcode = new DataMatrix();
barcode.Data = "(8101)06789900799";
barcode.FNC1Mode = DataMatrixFNC1Mode.FirstPosition;
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-fnc1.png");



Data Matrix Structured Append mode

Data Matrix barcode ISO standard provides a mechanism for the data in a file to be split into blocks and be represented in multiple Data Matrix barcode symbols.

  • Set property EnableStructuredAppend to true to enable Data Matrix Structured Append mode
  • Set property StructuredAppendCount to specify the total number of Data Matrix barcode symbols to store the same data message
  • Set property StructuredAppendIndex to specify the order of the current data matrix. The first symbol index should be 0. The C# sample code below will create the first Data Matrix symbol.
  • Set property StructuredAppendFileID with a random unique integer. All data matrix barcodes to store the same data message should have the same StructuredAppendFileID value.
DataMatrix barcode = new DataMatrix();
barcode.Data = "Data Matrix";
barcode.EnableStructuredAppend = true;
// The whole data message will be stored in three Data Matrix barcodes
barcode.StructuredAppendCount = 3;
// This Data Matrix symbol is the first one
barcode.StructuredAppendIndex = 0;
// All three Data Matrix barcodes must have the same file id
barcode.StructuredAppendFileID = 999; 
barcode.drawBarcode("C://BarcodeLib//csharp-data-matrix-structured-append.png");





How to Create Data Matrix in C# ASP.NET web app?

Our C# Data Matrix Barcodes Generator Library also allows users to generate Data Matrix in ASP.NET Web Project. The following are detailed steps:
  1. Add Barcodelib.Barcode.ASP.NET.dll to your web project.
  2. Add Barcodelib.Barcode.ASP.NET.dll to the Toolbox.
  3. Copy files "datamatrix.aspx" & "datamatrix.aspx.cs" from the folder barcode in the downloaded trial package to the folder where your web project is located.
  4. Drag DataMatrixASPNET from the toolbox to the split part of your aspx page, and then change its settings in the Properties window or with above C# source code.




How to Generate Data Matrix in C# WinForms?

You may use our C# Generator DLL to create a Data Matrix image in WinForms Project with following steps:
  1. Add BarcodeLib.Barcode.WinForms.dll from the downloaded trial package to your WinForms Project.
  2. Add BarcodeLib.Barcode.WinForms.dll to your Visual Studio Toolbox.
  3. Drag DataMatrixWinForm to your windows form and a Data Matrix barcode will be produced or you could use above free C# source code to create Data Matrix.




Supported Barcode Formats

C# 1D Barcode Generation:
C# 2D Barcode Generation:
Barcode Component for C# - Generates 1D Barcodes: Codabar, Code 11, Code 2 of 5, Code 39, Code 93, EAN-8, EAN-13, Interleaved 2 of 5, ITF-14 (UPC Shipping Container Symbol), Code 128, EAN 128/GS1 128, MSI Plessey, Intelligent Mail, Planet, Postnet, RM4SCC (British Royal Mail), UPC-A, UPC-E.
Barcode Component for C# - Generates 2D Barcodes: Data Matrix, PDF 417, QR Code.