How to Generate Code 128 Barcodes Using Aspose.BarCode for .NET
Code 128 is one of the most popular 1D barcode standards—used worldwide for shipping, logistics, inventory, asset tracking, and POS. It supports alphanumeric and numeric data, includes automatic checksum, and is widely compatible with scanners and industry software.
Real-World Problem
Organizations need to encode order numbers, asset IDs, or SKUs as robust barcodes for fast, error-free scanning. Manual barcode creation is inefficient and error-prone. Developers need a programmatic .NET approach to generate Code 128 barcodes that are accurate, readable, and compliant with industry requirements.
Solution Overview
Aspose.BarCode for .NET allows you to create and customize Code 128 barcodes for any application—batch shipping labels, inventory tags, or digital assets. All major code sets (A/B/C) are supported, and you can export images in multiple formats for both print and digital workflows.
Prerequisites
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.BarCode for .NET (install via NuGet)
- Basic C# knowledge
PM> Install-Package Aspose.BarCode
Step-by-Step Implementation
Step 1: Install and Import Aspose.BarCode
using Aspose.BarCode.Generation;
Step 2: Create the Code 128 Barcode Generator
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "A123B456789");
Step 3: (Optional) Set Code Set, Height, Caption, Colors
// Select code set automatically or force A/B/C (optional)
generator.Parameters.Barcode.Code128.Code128EncodeMode = Code128EncodeMode.Auto;
// Set bar height and X dimension (width)
generator.Parameters.Barcode.BarHeight.Pixels = 120;
generator.Parameters.Barcode.XDimension.Pixels = 3;
// Optional: Add human-readable caption
generator.Parameters.CaptionAbove.Text = "Order Number";
generator.Parameters.CaptionAbove.Visible = true;
generator.Parameters.CaptionBelow.Text = "A123B456789";
generator.Parameters.CaptionBelow.Visible = true;
// Optional: Set foreground/background colors
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.Barcode.BackColor = Color.White;
// Enable anti-aliasing for high-res output
generator.Parameters.ImageParameters.AntiAlias = true;
Step 4: Save or Export the Barcode Image
generator.Save("order-code128.png", BarCodeImageFormat.Png);
generator.Save("order-code128.svg", BarCodeImageFormat.Svg);
Step 5: Complete Example
using Aspose.BarCode.Generation;
using System.Drawing;
class Program
{
static void Main()
{
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "A123B456789");
generator.Parameters.Barcode.Code128.Code128EncodeMode = Code128EncodeMode.Auto;
generator.Parameters.Barcode.BarHeight.Pixels = 120;
generator.Parameters.Barcode.XDimension.Pixels = 3;
generator.Parameters.CaptionAbove.Text = "Order Number";
generator.Parameters.CaptionAbove.Visible = true;
generator.Parameters.CaptionBelow.Text = "A123B456789";
generator.Parameters.CaptionBelow.Visible = true;
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.Barcode.BackColor = Color.White;
generator.Parameters.ImageParameters.AntiAlias = true;
generator.Save("order-code128.png", BarCodeImageFormat.Png);
generator.Save("order-code128.svg", BarCodeImageFormat.Svg);
// Test with a barcode scanner or mobile app
}
}
Tips and Tricks
- Use Code128EncodeMode.CodeB for upper/lower case letters and numbers, or Code128EncodeMode.CodeC for numeric-only, double-density encoding.
- Export SVG for high-quality vector graphics in print or web layouts.
- Adjust bar height and XDimension to fit your label or packaging size requirements.
- Enable anti-aliasing for the smoothest images, especially for small barcodes or digital displays.
FAQs
Q: How do I encode long numeric values efficiently? A: Use Code128EncodeMode.CodeC for numeric-only data (length must be even) to double encoding density.
Q: Can I add a logo or overlay to the barcode image? A: Yes, you can post-process the image using System.Drawing or any .NET graphics library after saving.
Q: Does Code 128 include a checksum? A: Yes, checksum is calculated and encoded automatically.
Use Cases and Applications
- Shipping and carton labels
- Inventory/asset tracking
- Retail/POS product tags
- Laboratory/sample tracking
- Library and file management
Best Practices
- Always validate output with a scanner before mass printing
- Use sufficient bar height for easy handheld scanning
- Choose SVG or 300 DPI PNG for professional print labels
- Log or store code text and barcode image path for audit trails
Conclusion
Aspose.BarCode for .NET is the ideal toolkit for generating Code 128 barcodes at scale, with rich customization, high-quality output, and compliance for all industry scenarios. For more options, see the Aspose.BarCode API Reference .