How to Create DataMatrix Barcode in C#

A DataMatrix code is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern. The information to be encoded can be text or numeric data.

Following code snippets show how to create a DataMatrix barcode image using Spire.Barcode.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the width of barcode module

settings.X = 2;

Step 3: Set the barcode type as DataMatrix.

settings.Type = BarCodeType.DataMatrix;

Step 4: Set the data and display text

settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";

Step 5: Set the DataMatrix symbol shape to square.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;

Step 6: Generate barcode image based on the settings and save it in .png format.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);

How to Create DataMatrix Barcode in C#

To create a rectangular DataMatrix barcode, set the DataMatrixSymbolShape property to Rectangle.

settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;

How to Create DataMatrix Barcode in C#

Full Code:

BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.DataMatrix;
settings.X = 1.5f;
settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;
//rectangular DataMatrix barcode
//settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;  
settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);