跳過到頁腳內容
使用IRONBARCODE

如何在C#中生成Code 128條碼

條碼在現代商業運作中至關重要,從庫存管理到產品標籤和運輸。 Code 128 是各種條碼代碼集中一個用途廣泛且廣泛使用的選項。 在這篇文章中,我們將探索如何使用 IronBarcode 庫在 C# 中構建 Code 128 條碼生成器。

如何在 C# 中生成 Code 128 條碼

  1. 安裝 IronBarcode 庫
  2. 使用 Code 128 編碼生成條碼
  3. 調整條碼大小
  4. 通過更改背景和條碼顏色來美化條碼
  5. 讀取已創建的條碼

Code 128 條碼介紹

Code 128 代碼集是一種高密度、可變長度的線性條碼,可以編碼字母數字數據和特殊字符。 它是自檢的,包括一個校驗和位以確保數據的準確性。 Code 128 編碼方案支持三個控制字符:

  1. 字符集 A: 包括大寫字母、數字和特殊字符。
  2. 字符集 B: 包括大寫字母、小寫字母、數字和其他特殊字符。
  3. 字符集 C: 編碼數字組(00 到 99)。

為什麼選擇 IronBarcode?

IronBarcode 是一個強大的 .NET 庫,方便條碼生成、解碼和自定義。 With support for various Barcode Encoding like Code 128, Code 39, Code 93, Code EAN 13, EAN 8, QR codes, and others. 它提供直觀的 API 用於內容、大小和外觀調整。 其解碼能力、自動校驗和計算以及 圖像導出 使其成為開發人員在庫存管理及其他方面的寶貴工具。 該庫在屬性、邊距、字體和顏色的自定義選項增強了其在與條碼相關的任務中的多功能性。

在 C# 中創建 Code 128 條碼生成器

現在,我們將編寫代碼在 C# 中生成 Code 128 條碼圖像。 第一步是將 IronBarcode 庫安裝到我們的項目中。 項目可以是任何類型,如 Windows 窗體、Web 窗體、MAUI、Xamarin、ASP.NET MVC、Razor 或 Blazor 項目。

安裝 IronBarcode 庫

要使用 Visual Studio 中的包管理控制台安裝IronBarcode NuGet 包,您可以按照以下步驟進行:

  1. 打開 Visual Studio。
  2. 在頂部菜單中,轉至“查看”>“其他窗口”>“包管理器控制台”以打開包管理器控制台。
  3. 在包管理器控制台中,您可以使用 Install-Package 命令來安裝 IronBarcode 包。 鍵入以下命令並按 Enter:

    Install-Package BarCode
    Install-Package BarCode
    SHELL
  4. 此命令將下載並安裝 IronBarcode NuGet 包的最新版本及其依賴項到您的項目中。

添加以下命名空間以在項目中使用條碼庫。

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

生成 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")
$vbLabelText   $csharpLabel

此代碼從輸入字符串創建條碼並將其保存為名為“myBarcode.Jpeg”的 JPEG 圖像文件。 使用的特定編碼是 Code 128,可以表示字母數字字符。

解釋

第一行代碼創建一個名為 myBarcode 的新變量。 它使用 BarcodeWriter.CreateBarcode 方法基於輸入字符串“12345ABC12345”生成條碼。

第二個參數 BarcodeWriterEncoding.Code128 指定條碼的編碼類型。 在這種情況下,它使用了 Code 128 編碼,即常用於字母數據。 生成的條碼存儲在 myBarcode 變量中。

第二行將生成的條碼保存為 JPEG 圖像文件。 保存的圖像的文件名為“myBarcode.Jpeg”。 保存的圖像格式為 JPEG(聯合圖像專家組)。

輸出

生成的條碼如下所示:

如何生成 Code 128 條碼:圖 1 - 前述代碼輸出的條碼

此代碼現在可以使用條碼讀取設備進行讀取。

現在,讓我們調整我們的條碼大小。

調整條碼大小

以下代碼將根據給定的尺寸大小調整我們的條碼。

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
$vbLabelText   $csharpLabel

創建和保存條碼的代碼保持不變。 只需增加額外的行來調整條碼大小。

ResizeTo() 方法調整存儲在 myBarcode 變量中的條碼圖像的大小。 ResizeTo 方法被調用在 myBarcode 對象上。 傳遞給 ResizeTo 的兩個參數是寬度和高度。 在這種情況下,寬度設置為 800 像素,高度設置為 300 像素。

這樣,我們可以設置最小寬度和條形模塊高度。 結果條碼圖像將在調整大小後具有以下尺寸,如下所示。

條碼圖像

如何生成 Code 128 條碼:圖 2 - 前述代碼調整大小後的條碼

現在,讓我們美化我們的條碼。

美化 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
$vbLabelText   $csharpLabel

生成和保存條碼的代碼是相同的。 我只增加了兩行來改變背景和條碼顏色。 解釋如下:

  • ChangeBackgroundColor: 方法 ChangeBackgroundColor 被調用在 myBarcode 對象上。 此方法更改條碼圖像的背景顏色。 傳遞給 ChangeBackgroundColor 的參數是 IronSoftware.Drawing.Color.Cornsilk,指定所需的背景顏色。 在這種情況下,背景顏色設置為 Cornsilk,是一種淡黃色。

  • ChangeBarCodeColor: 方法 ChangeBarCodeColor 被調用在 myBarcode 對象上。 此方法更改條碼條的顏色。 傳遞給 ChangeBarCodeColor 的參數是 IronSoftware.Drawing.Color.Brown,指定所需的條碼顏色。 在這種情況下,條碼顏色設置為棕色。

輸出

我們美化的條碼如下所示:

如何生成 Code 128 條碼:圖 3 - 前述代碼輸出的美化條碼

讀取 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
$vbLabelText   $csharpLabel

上面的代碼從“myBarcode.Jpeg”圖像文件中讀取條碼並將其值打印到控制台。 BarcodeReader 類負責從圖像中解碼條碼數據。 代碼的解釋如下:

解釋 of Code

  • 第一行創建一個名為 resultFromBarcode 的變量。 它調用 BarcodeReader.Read 方法從文件名為“myBarcode.Jpeg”的圖像文件中讀取條碼。 此操作的結果存儲在 resultFromBarcode 變量中。

  • 第二行開始一個迴圈,遍歷 resultFromBarcode 集合中的每個條碼值。 foreach 迴圈使我們能夠逐一處理每個條碼值。

  • 在迴圈內,這一行將每個條碼值打印到控制台。 barcodeValue 表示從圖像中讀取的條碼的內容。

條碼值將在控制台上顯示如下。

輸出

如何生成 Code 128 條碼:圖 4 - 從讀取的條碼中得到的控制台輸出

結論

總之,這篇文章展示了如何使用 IronBarcode 庫在 C# 中創建 Code 128 條碼生成器。 By leveraging the capabilities of IronBarcode, developers can easily generate, customize, and style Code 128 barcodes for various applications, including inventory management, product labeling, and shipping. 通過遵循本教程,開發人員可以將強大的條碼功能集成到他們的 C# 項目中,提高在處理條碼生成和解碼相關任務時的效率。 IronBarcode 的多功能性和直觀的 API 使其成為開發涉及條碼實施的應用程序的開發人員的寶貴工具。

在使用 IronBarcode 庫生成 Code 128 條碼的過程中,開發人員可以自由選擇自定義代碼集字符,確保最佳編碼具有不同 ASCII 值的數據。 渲染代碼無縫適應首選的圖像格式,為開發人員提供了將條碼保存為 JPEG 或其他格式的選擇。 此外,包含的停止字符確保了生成條碼中的編碼信息的準確終止。

IronBarcode 提供免費試用,以解鎖該庫滿足其開發需求的全部潛力。 這種方法允許開發人員在購買商業許可證之前評估 IronBarcode 的能力。

常見問題解答

如何在 C# 中生成代碼128條碼?

要在 C# 中生成代碼128條碼,使用 IronBarcode 庫中的 BarcodeWriter.CreateBarcode 方法,提供所需的輸入字符串並指定代碼128作為編碼類型。然後,您可以使用 SaveAsJpeg 等方法導出生成的條碼圖像。

代碼128用於什麼用途?

代碼128用於在緊湊的條碼格式中編碼字母數字數據和特殊字符。由於其高數據密度和靈活性,非常適合於庫存管理、產品標籤和運輸應用。

我可以使用 IronBarcode 自定義條碼的外觀嗎?

是的,您可以通過修改條碼顏色 ChangeBackgroundColorChangeBarCodeColor 並使用 ResizeTo 方法調整其大小來自定義條碼的外觀。

如何在 C# 中讀取條碼?

要在 C# 中讀取條碼,使用 IronBarcode 庫的 BarcodeReader.Read 方法。此方法處理包含條碼的圖像文件並返回解碼值以供進一步處理。

使用 IronBarcode 庫進行條碼生成的優勢是什麼?

IronBarcode 庫提供了用戶友好的 API,支持各種條碼編碼,提供自動校驗和計算,並允許將圖像導出為多種格式,使其成為條碼生成和自定義的靈活高效工具。

IronBarcode 可以導出為哪些圖像格式?

IronBarcode 可以將條碼圖像導出為多種格式,包括 JPEG,這允許靈活地處理和集成條碼圖像到不同的應用程序中。

在購買之前可以試用 IronBarcode 嗎?

是的,您可以試用 IronBarcode 的免費試用版,充分探索其潛力和功能,然後再選擇商業許可證,確保您能夠在 C# 專案中有效地集成條碼功能。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。