How to Generate Data Matrix Barcodes Using Aspose.BarCode for .NET
This article explains how to generate Data Matrix barcodes in C# using Aspose.BarCode for .NET. Data Matrix is a compact, high-capacity 2D barcode used in manufacturing, logistics, pharmaceuticals, electronics, and healthcare for tracking, labeling, and compliance.
Real-World Problem
Organizations need to encode a lot of data—product IDs, batch numbers, serials, or logistics info—into a tiny, scannable barcode. Data Matrix is the standard for many regulatory, industrial, and healthcare applications. Manual or generic barcode tools rarely provide the automation or reliability that .NET developers need.
Solution Overview
Aspose.BarCode for .NET enables you to generate Data Matrix barcodes programmatically with customizable size, error correction, and output options—ideal for labeling parts, packaging, samples, and more.
Prerequisites
Before you start, ensure you have:
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.BarCode for .NET installed via NuGet
- Basic knowledge of C#
PM> Install-Package Aspose.BarCode
Step-by-Step Implementation
Step 1: Install and Import Aspose.BarCode
Install the NuGet package and import the required namespaces:
using Aspose.BarCode.Generation;
Step 2: Create the Data Matrix Generator
Instantiate the generator for Data Matrix:
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DataMatrix, "LOT2025-ABCDEFG");
Step 3: Customize Data Matrix Settings
Adjust size, ECC level, or style as needed:
// Set module (pixel) size
generator.Parameters.Barcode.XDimension.Pixels = 6;
// Optional: Set Data Matrix ECC level (ECC200 recommended)
generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.ECC200;
// Optional: Set Data Matrix size/version
generator.Parameters.Barcode.DataMatrix.DataMatrixVersion = DataMatrixVersion.Auto;
// Optional: Set foreground and background color
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.Barcode.BackColor = Color.White;
Step 4: Generate and Save the Data Matrix Barcode
Export the barcode to PNG, JPEG, or any supported format:
generator.Save("data-matrix.png", BarCodeImageFormat.Png);
Step 5: Complete Example
using Aspose.BarCode.Generation;
using System.Drawing; // Required for Color
class Program
{
static void Main()
{
// Create Data Matrix generator for a lot/batch number
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DataMatrix, "LOT2025-ABCDEFG");
generator.Parameters.Barcode.XDimension.Pixels = 6;
generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.ECC200;
generator.Parameters.Barcode.DataMatrix.DataMatrixVersion = DataMatrixVersion.Auto;
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.Barcode.BackColor = Color.White;
generator.Save("data-matrix.png", BarCodeImageFormat.Png);
// Test output with a Data Matrix scanner
}
}
Use Cases and Applications
- Logistics & supply chain: Item-level tracking on boxes, pallets, shipments
- Pharmaceutical labeling: Regulatory compliance, serialization, traceability
- Manufacturing: Electronic parts, boards, samples, or inventory
Common Challenges and Solutions
Challenge 1: Data Matrix won’t scan? Solution: Use a clear, high-contrast color scheme; verify minimum module size for your scanner.
Challenge 2: Too much data for a small code? Solution: Increase barcode module size or split data across multiple barcodes.
Challenge 3: Regulatory compliance? Solution: Use ECC200, log code text and parameters, test output with certified scanners.
Performance Considerations
- Batch generate Data Matrix barcodes for all items in inventory/production
- Use proper ECC and version for the data length/criticality
- Export in high resolution for reliable scanning
Best Practices
- Always use ECC200 for industry compatibility
- Test code output with physical scanners and regulatory systems
- Export to PNG or SVG for print quality
- Keep code text concise when possible
Advanced Scenarios
1. Batch Generate Data Matrix Barcodes
foreach (var item in items)
{
BarcodeGenerator g = new BarcodeGenerator(EncodeTypes.DataMatrix, item.SerialNumber);
g.Save($"{item.SerialNumber}.png", BarCodeImageFormat.Png);
}
2. Set Specific Data Matrix Size
generator.Parameters.Barcode.DataMatrix.DataMatrixVersion = DataMatrixVersion.Rows24Columns24;
Conclusion
Aspose.BarCode for .NET lets you create Data Matrix barcodes for any modern workflow—compliance, logistics, pharma, or industrial. For more features, visit the Aspose.BarCode API Reference .