Generating Code 128 barcodes programmatically is essential for supply chain management, shipping logistics, inventory control, and asset tracking. This article demonstrates how to use Aspose.BarCode for .NET to create accurate and readable barcodes with customizable properties.
Introduction
Code 128 is a versatile barcode standard widely used in various industries for its high data density and automatic checksum feature. In this guide, we will explore how to generate Code 128 barcodes using Aspose.BarCode for .NET, ensuring they are accurate, readable, and compliant with industry standards.
Prerequisites
Before diving into the implementation details, ensure you have the following setup:
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.BarCode for .NET installed via NuGet package manager
- Basic knowledge of C#
To install Aspose.BarCode, run the following command in your Package Manager Console:
PM> Install-Package Aspose.BarCode
Step-by-Step Implementation
Step 1: Import Aspose.BarCode Namespace
Import the necessary namespace to work with barcodes.
using Aspose.BarCode.Generation;
Step 2: Create a Barcode Generator Instance
Create an instance of BarcodeGenerator
and specify the barcode type as Code128. You can also set the data to be encoded in this step.
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "A123B456789");
Step 3: Customize Barcode Properties (Optional)
Customize various properties of the barcode such as code set mode, bar height, caption text, and 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
Save the generated barcode image in desired formats such as PNG, SVG, etc.
generator.Save("order-code128.png", BarCodeImageFormat.Png);
generator.Save("order-code128.svg", BarCodeImageFormat.Svg);
Complete Example
Below is a complete example demonstrating how to generate and customize Code 128 barcodes using Aspose.BarCode for .NET.
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);
}
}
Tips and Tricks
- Use
Code128EncodeMode.CodeB
for upper/lower case letters and numbers, orCode128EncodeMode.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.
More in this category
- Create GS1-128 (UCC/EAN-128) Barcodes for Supply Chain and Compliance in .NET
- Create ISBN and ISSN Barcodes for Books and Periodicals in .NET
- Create UPC-A and UPC-E Barcodes for Product Packaging in .NET
- Extract QR Code Metadata Using Aspose.BarCode for .NET
- Generate Aztec Codes Using Aspose.BarCode for .NET