How to Generate Code 128 barcode in C#
条形码在现代商业运营中至关重要,从库存管理到产品标签和运输。 Code 128 在各种条码代码集中的表现出色,是一种灵活且广泛使用的选项。 在本文中,我们将探索如何在C#中使用IronBarcode库构建一个Code 128条形码生成器。
如何在C#中生成Code 128条形码
- 安装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用于内容、大小和外观的调整。 其解码能力、自动校验和计算以及图像导出使其成为库存管理等领域开发人员的宝贵工具。 该库对属性、边距、字体和颜色的自定义选项增强了其在条形码相关任务中的多功能性。
在C#中创建Code 128条形码生成器
现在,我们将编写代码在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变量中的条形码图像的大小。 ResizeTo方法是在myBarcode对象上调用的。 传递给ResizeTo的两个参数是宽度和高度。 在这种情况下,宽度设置为800像素,高度设置为300像素。
通过这种方式,我们可以设置最小宽度和条码模块的高度。 调整大小后的条形码图像将具有这些尺寸,如下所示。
条码图像

现在,让我们设置条形码的样式。
设置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:
ChangeBackgroundColor方法是在myBarcode对象上调用的。 此方法更改条码图像的背景颜色。 传递给ChangeBackgroundColor的参数是Iron Software.Drawing.Color.Cornsilk,指定了所需的背景颜色。 在这种情况下,背景颜色设置为Cornsilk,它是一种淡黄色颜色。- ChangeBarCodeColor:
ChangeBarCodeColor方法是在myBarcode对象上调用的。 此方法更改条码条的颜色。 传递给ChangeBarCodeColor的参数是Iron Software.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表示从图像中读取的条码的内容。
条码值将打印在控制台上,如下所示。
输出

结论
总之,本文展示了如何使用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#项目中。







