How to Generate Code 128 barcode in C#
条形码在现代商业运营中至关重要,从库存管理到产品标签和运输。 Code 128 在各种条码代码集中的表现出色,是一种灵活且广泛使用的选项。 在本文中,我们将探索如何在C#中使用IronBarcode库构建一个Code 128条形码生成器。
How to Generate Code 128 Barcode in C#
- 安装IronBarcode库
- 使用Code 128编码生成条形码
- 调整条形码大小
- 通过更改背景色和条形码颜色来设置条形码样式
- 读取创建的条形码
Code 128条形码简介
Code 128代码集是一种高密度、可变长度的线性条码,可以编码字母数字数据和特殊字符。 它是自检的,包括一个校验位以确保数据准确性。 Code 128编码方案支持三个控制字符:
- 字符集A:包括大写字母、数字和特殊字符。
- 字符集B:包括大写字母、小写字母、数字和额外的特殊字符。
- 字符集C:编码数字对(00至99)。
为什么选择IronBarcode?
IronBarcode是一个强大的.NET库,可以简化条形码的生成、解码和自定义。 支持各种条形码编码,如Code 128、Code 39、Code 93、Code EAN 13、EAN 8、QR码等。 提供直观的API用于内容、大小和外观的调整。 其解码能力、自动校验和计算以及图像导出使其成为库存管理等领域开发人员的宝贵工具。 该库对属性、边距、字体和颜色的自定义选项增强了其在条形码相关任务中的多功能性。
Creating Code 128 Barcode Generator in C#
现在,我们将编写代码在C#中生成一个Code 128条形码图像。 第一步是将IronBarcode库安装到我们的项目中。 该项目可以是任何类型,如Windows Forms、Web Forms、MAUI、Xamarin、ASP.NET MVC、Razor或Blazor项目。
安装IronBarcode库
要使用Visual Studio中的包管理器控制台安装IronBarcode NuGet包,您可以按照以下步骤操作:
- 打开 Visual Studio。
- 在顶部菜单中,转到"查看" > "其他窗口" > "包管理器控制台"以打开包管理器控制台。
-
在软件包管理器控制台中,您可以使用
Install-Package命令安装 IronBarcode 软件包。 键入以下命令并按Enter键:Install-Package BarCodeInstall-Package BarCodeSHELL - 此命令将下载并安装最新版本的IronBarcode NuGet包及其依赖项到您的项目中。
添加以下命名空间以在项目中使用条码库。
using IronBarCode;
using IronBarCode;
Imports IronBarCode
生成Code 128条形码图像
以下代码将生成一个Code 128条形码。
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
' Create a barcode from the input string and specify encoding type as Code 128
Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)
' Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg")
此代码根据输入字符串创建条形码,并将其保存为名为"myBarcode.Jpeg"的JPEG图像文件。使用的特定编码是Code 128,可以表示字母数字字符。
解释
第一行代码创建了一个名为 myBarcode 的新变量。 它使用 BarcodeWriter.CreateBarcode 方法根据输入字符串"12345ABC12345"生成条形码。
第二个参数 BarcodeWriterEncoding.Code128,指定条形码的编码类型。 在这种情况下,它使用Code 128编码,通常用于字母数字数据。 生成的条形码存储在 myBarcode 变量中。
第二行将生成的条码保存为JPEG图像文件。保存的图像的文件名是"myBarcode.Jpeg"。 保存的图像格式是JPEG(联合图像专家组)。
输出
生成的条形码如下:

现在,这段代码可以用条码读取设备读取。
现在,让我们调整条形码的大小。
调整条码大小
以下代码将根据给定尺寸调整我们的条形码大小。
static void Main(string[] args)
{
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300);
// Save the resized barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
static void Main(string[] args)
{
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300);
// Save the resized barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
Shared Sub Main(ByVal args() As String)
' Create a barcode from the input string and specify encoding type as Code 128
Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)
' Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300)
' Save the resized barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg")
End Sub
创建和保存条形码的代码保持不变。 只需添加额外的一行来调整条码大小。
ResizeTo() 方法调整存储在 myBarcode 变量中的条形码图像的大小。 在 myBarcode 对象上调用方法 ResizeTo。 传递给 ResizeTo 的两个参数是宽度和高度。 在这种情况下,宽度设置为800px,高度设置为300px。
通过这种方式,我们可以设置最小宽度和条码模块的高度。 调整大小后的条形码图像将具有这些尺寸,如下所示。
条码图像

现在,让我们设置条形码的样式。
设置Code 128条形码样式
现在,让我们通过改变背景颜色和条形码颜色来设置条码样式。
static void Main(string[] args)
{
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300);
// Change the background color of the barcode
myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk);
// Change the barcode color
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown);
// Save the styled barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
static void Main(string[] args)
{
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);
// Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300);
// Change the background color of the barcode
myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk);
// Change the barcode color
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown);
// Save the styled barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
Shared Sub Main(ByVal args() As String)
' Create a barcode from the input string and specify encoding type as Code 128
Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)
' Resize the barcode image to the specified width and height (in pixels)
myBarcode.ResizeTo(800, 300)
' Change the background color of the barcode
myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk)
' Change the barcode color
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown)
' Save the styled barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg")
End Sub
生成和保存条形码的代码相同。 我只是添加了两行额外的代码以更改背景和条码颜色。 具体解释如下:
-
ChangeBackgroundColor:在
myBarcode对象上调用方法ChangeBackgroundColor。 此方法更改条码图像的背景颜色。 传递给ChangeBackgroundColor的参数是IronSoftware.Drawing.Color.Cornsilk,它指定了所需的背景颜色。 在这种情况下,背景颜色设置为Cornsilk,它是一种淡黄色颜色。 - ChangeBarCodeColor:在
myBarcode对象上调用方法ChangeBarCodeColor。 此方法更改条码条的颜色。 传递给ChangeBarCodeColor的参数是IronSoftware.Drawing.Color.Brown,它指定所需的条形码颜色。 在这种情况下,条码颜色设置为棕色。
输出
我们的样式化条形码如下:

读取Code 128条形码
我们已经学会了如何生成Code 128条形码。 让我们编写代码以读取条码:
static void Main(string[] args)
{
// Read barcodes from the specified image file
var resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg");
// Loop through each barcode value read from the image
foreach (var barcodeValue in resultFromBarcode)
{
// Print each barcode value to the console
Console.WriteLine(barcodeValue);
}
}
static void Main(string[] args)
{
// Read barcodes from the specified image file
var resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg");
// Loop through each barcode value read from the image
foreach (var barcodeValue in resultFromBarcode)
{
// Print each barcode value to the console
Console.WriteLine(barcodeValue);
}
}
Shared Sub Main(ByVal args() As String)
' Read barcodes from the specified image file
Dim resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg")
' Loop through each barcode value read from the image
For Each barcodeValue In resultFromBarcode
' Print each barcode value to the console
Console.WriteLine(barcodeValue)
Next barcodeValue
End Sub
上面的代码从"myBarcode.Jpeg"图像文件中读取条码,并将其值打印到控制台。 BarcodeReader 类负责解码图像中的条形码数据。 代码的解释如下:
代码解释
-
第一行创建了一个名为
resultFromBarcode的变量。 它调用BarcodeReader.Read方法从名为"myBarcode.Jpeg"的图像文件中读取条形码。 该操作的结果存储在resultFromBarcode变量中。 -
第二行开始一个循环,遍历
resultFromBarcode集合中的每个条形码值。foreach循环允许我们逐个处理每个条形码值。 - 循环内,此行将每个条码值打印到控制台。
barcodeValue表示从图像中读取的条形码的内容。
条码值将打印在控制台上,如下所示。
输出

分析代码 128 编码段
如前所述,Code 128 使用 A、B 和 C 三种字符集,并在它们之间切换以高效地对数据进行编码。 IronBarcode 提供了 Code128GS1Parser.GetEncodingInfo 方法,用于分析给定输入字符串使用的字符集。
// Analyze Code 128 encoding segments
var result = Code128GS1Parser.GetEncodingInfo("ABC123456DEF");
Console.WriteLine(result.CharacterSetSummary); // "B → C → B"
Console.WriteLine(result.TotalSymbols); // 14
Console.WriteLine(result.IsGS1); // false
foreach (var segment in result.Segments)
{
Console.WriteLine($"{segment.CharacterSetName}: \"{segment.Data}\" ({segment.SymbolCount} symbols)");
}
// Output:
// Code B: "ABC" (3 symbols)
// Code C: "123456" (3 symbols)
// Code B: "DEF" (3 symbols)
// Analyze Code 128 encoding segments
var result = Code128GS1Parser.GetEncodingInfo("ABC123456DEF");
Console.WriteLine(result.CharacterSetSummary); // "B → C → B"
Console.WriteLine(result.TotalSymbols); // 14
Console.WriteLine(result.IsGS1); // false
foreach (var segment in result.Segments)
{
Console.WriteLine($"{segment.CharacterSetName}: \"{segment.Data}\" ({segment.SymbolCount} symbols)");
}
// Output:
// Code B: "ABC" (3 symbols)
// Code C: "123456" (3 symbols)
// Code B: "DEF" (3 symbols)
Imports System
' Analyze Code 128 encoding segments
Dim result = Code128GS1Parser.GetEncodingInfo("ABC123456DEF")
Console.WriteLine(result.CharacterSetSummary) ' "B → C → B"
Console.WriteLine(result.TotalSymbols) ' 14
Console.WriteLine(result.IsGS1) ' False
For Each segment In result.Segments
Console.WriteLine($"{segment.CharacterSetName}: ""{segment.Data}"" ({segment.SymbolCount} symbols)")
Next
' Output:
' Code B: "ABC" (3 symbols)
' Code C: "123456" (3 symbols)
' Code B: "DEF" (3 symbols)
该方法返回一个 Code128EncodingInfo 对象,其中包含编码段的分解信息。 在这个例子中,编码器先用代码 B 编码字母,然后用代码 C 编码数字对(数字最紧凑的编码),最后再用代码 B 编码剩余的字母。 TotalSymbols 计数包括起始符号、数据符号、代码开关、校验位和停止符号。
结论
总之,本文展示了如何使用IronBarcode库在C#中创建Code 128条码生成器。 通过利用IronBarcode的功能,开发人员可以轻松生成、自定义和设置样式Code 128条码,用于各种应用,包括库存管理、产品标签和运输。 通过遵循本教程,开发人员可以将强大的条码功能集成到其C#项目中,提高条码生成和解码相关任务的处理效率。 IronBarcode的多功能性和直观的API使其成为在条码实施中涉及的应用程序开发人员的宝贵工具。
在使用IronBarcode库生成Code 128条形码的过程中,开发人员可以灵活自定义代码集选择字符,从而确保以最佳的方式对具有不同ASCII值的数据进行编码。 渲染代码可以无缝适应首选的图像格式,为开发人员提供将条形码保存为JPEG或其他格式的选择。 此外,包含停止字符确保了生成的条码中编码信息的精确终止。
IronBarcode提供免费试用,以解锁该库在其开发需求中的全部潜力。 这种方法使开发人员能够在提交商业许可证之前评估IronBarcode的功能。
常见问题解答
如何在C#中生成代码128条形码?
要在C#中生成代码128条形码,请使用IronBarcode库中的BarcodeWriter.CreateBarcode方法,输入您所需的字符串,并指定代码128作为编码类型。然后可以使用SaveAsJpeg等方法导出生成的条形码图像。
代码128用于什么?
代码128用于以紧凑条形码格式编码字母数字数据和特殊字符。由于其高数据密度和多功能性,非常适合库存管理、产品标签和运输应用。
我可以使用IronBarcode自定义条形码的外观吗?
可以,通过修改颜色使用ChangeBackgroundColor和ChangeBarCodeColor,并使用ResizeTo方法调整大小,您可以使用IronBarcode自定义条形码的外观。
如何在C#中读取条形码?
要在C#中读取条形码,请使用IronBarcode库中的BarcodeReader.Read方法。此方法处理包含条形码的图像文件,并返回解码值以供进一步处理。
使用IronBarcode库生成条形码的优势是什么?
IronBarcode库提供用户友好的API,支持多种条形码编码,提供自动校验和计算,并允许以多种格式导出图像,使其成为条形码生成和自定义的灵活而高效的工具。
IronBarcode可以导出到哪些图像格式?
IronBarcode可以将条形码图像导出到多种格式,包括JPEG,这使其在不同应用中处理和整合条形码图像时具有灵活性。
可以在购买前试用IronBarcode吗?
可以,您可以试用IronBarcode的免费试用版,探索其全部潜力和能力,然后再决定购买商业许可证,确保能够将条形码功能有效集成到您的C#项目中。

