跳至頁尾內容
使用IRONBARCODE
如何在C#中生成Code 128條碼 | IronBarcode

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

條碼在現代商業運作中至關重要,從庫存管理到產品標籤和運輸都能見其蹤影。 Code 128在各種條碼編碼集中脫穎而出,成為一個多用途且廣泛使用的選擇。 在這篇文章中,我們將探索如何使用IronBarcode程式庫在C#中構建一個Code 128條碼生成器。

How to Generate Code 128 Barcode in C

  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程式庫,可方便條碼生成、解碼和自定義。 支持各種條碼編碼,如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套件,您可以按照以下步驟操作:

  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(Joint Photographic Experts Group)。

輸出

生成的條碼如下:

如何在C#中生成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

建立和保存條碼的程式碼保持不變。 只需增加一行來調整條碼大小。

myBarcode變數中的條碼圖像。 myBarcode物件上被調用。 傳遞給ResizeTo的兩個參數是寬度和高度。 在這種情況下,寬度設為800像素,高度設為300像素。

透過這種方式,我們可以設置最小寬度和條模長度。 調整大小後的條碼圖像將具有如下尺寸。

條碼圖像

如何在C#中生成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方法。 此方法會更改條碼圖像的背景顏色。 傳遞給IronSoftware.Drawing.Color.Cornsilk,它指定了所期望的背景顏色。 在這種情況下,背景顏色設為Cornsilk,這是一種淡黃色。

  • 改變條碼顏色:ChangeBarCodeColor方法。 此方法會更改條碼的顏色。 傳遞給IronSoftware.Drawing.Color.Brown,它指定了所期望的條碼顏色。 在這種情況下,條碼顏色設為Brown。

輸出

我們設計好的條碼如下:

如何在C#中生成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類負責從圖像中解碼條碼資料。 程式碼的說明如下:

程式碼說明

  • 第一行建立了一個名為resultFromBarcode的變數。 它調用BarcodeReader.Read方法從名為"myBarcode.Jpeg"的圖像文件中讀取條碼。 該操作的結果儲存在resultFromBarcode變數中。

  • 第二行開始一個迴圈,迭代resultFromBarcode集合中的每個條碼值。 foreach迴圈讓我們逐個處理每個條碼值。

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

條碼值將在控制台上以如下方式列印。

輸出

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

分析Code 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)
$vbLabelText   $csharpLabel

該方法返回一個包含編碼段細目表的Code128EncodingInfo物件。 在此範例中,編碼器從字母的Code B切換到數字對(數字的最緊湊編碼)的Code C,然後再切換回剩餘字母的Code B。 TotalSymbols計數包括起始符號、資料符號、程式碼切換、校驗位和終止符號。

結論

總結一下,本文示範了如何使用IronBarcode程式庫在C#中建立一個Code 128條碼生成器。 透過利用IronBarcode的功能,開發者可以輕鬆生成自定義設計Code 128條碼,以用於包含庫存管理、產品標簽和運輸的各種應用中。 按照本教程,開發人員可以將強大的條碼功能整合到他們的C#專案中,提高在條碼生成和解碼相關工作中的效率。 IronBarcode的多功能性和直觀的API使其成為從事條碼實施應用的開發者的一個寶貴工具。

在使用IronBarcode程式庫生成Code 128條碼的過程中,開發人員可以靈活地自定義程式碼集選擇字元,確保以最佳方式編碼具有不同ASCII值的資料。 渲染程式碼能夠無縫適應所需的圖像格式,提供開發者將條碼保存為JPEG或其他格式的選擇。 此外,包含終止字元確保了生成條碼中編碼資訊的準確終止。

IronBarcode提供免費試用來解鎖該程式庫的完整潛力,以滿足他們的開發需求。 此方法使開發人員在承諾購買商業授權之前,可以評估IronBarcode的功能。

常見問題

我怎麼在C#中生成Code 128條碼?

要在C#中生成Code 128條碼,使用IronBarcode程式庫中的BarcodeWriter.CreateBarcode方法,使用您所需的輸入字串並指定Code 128為編碼型別。然後可以使用SaveAsJpeg等方法導出生成的條碼影像。

Code 128是用來做什麼的?

Code 128用於在緊湊的條碼格式中編碼字母數字資料和特殊字元。由於其高資料密度和多功能性,它特別適合於庫存管理、產品標籤和運輸應用。

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

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

我如何在C#中讀取條碼?

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

使用IronBarcode程式庫生成條碼有什麼優勢?

IronBarcode程式庫提供了使用者友好的API,支援多種條碼編碼,提供自動校驗和允許以多種格式導出影像,使其成為一個靈活高效的條碼生成和自定義工具。

IronBarcode可以導出到哪些影像格式?

IronBarcode可以將條碼影像導出到各種格式,包括JPEG,這樣在處理和將條碼影像整合到不同應用中時就有靈活性。

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

可以,您可以試用IronBarcode的免費試用版,探索其全部潛力和功能,然後再決定是否購買商業授權,以確保您能有效地將條碼功能整合到C#專案中。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話