如何在 C# 中生成 Code 128 條碼
條碼在現代商業營運中至關重要,從庫存管理到產品標籤和運輸都離不開它。 在各種條碼代碼集中, Code 128因其多功能性和廣泛應用而脫穎而出。 在本文中,我們將探討如何使用IronBarcode庫在 C# 中建立 Code 128 條碼產生器。
How to Generate Code 128 Barcode in C#
- 安裝 IronBarcode 庫
- 使用 Code 128 編碼產生條碼
- 調整條碼大小
- 透過變更背景和條碼顏色來設定條碼樣式
- 讀取產生的條碼
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。
- 在頂部選單中,前往"檢視">"其他視窗">"套件管理員控制台"以開啟套件管理員控制台。
-
在軟體套件管理器控制台中,您可以使用
Install-Package指令安裝 IronBarcode 軟體套件。 輸入以下指令並按下回車鍵: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(聯合影像專家小組)。
輸出
產生的條碼如下:
如何在 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
建立和保存條碼的程式碼保持不變。 只需新增一行即可調整條碼大小。
ResizeTo() 方法調整儲存在 myBarcode 變數中的條碼影像的大小。 在 myBarcode 物件上呼叫方法 ResizeTo。 傳遞給 ResizeTo 的兩個參數是寬度和高度。 在這種情況下,寬度設定為 800 像素,高度設定為 300 像素。
這樣我們就可以設定最小寬度和欄模組高度。 調整大小後,產生的條碼影像將具有如下所示的尺寸。
條碼影像
如何在 C# 中產生 Code 128 條碼:圖 2 - 根據先前代碼調整大小後的條碼
現在,讓我們來設計條碼樣式。
款式代碼 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,它指定了所需的背景顏色。 在這種情況下,背景顏色設定為玉米須色,這是一種淡黃色。 - 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 方法調整其大小來自定義條碼的外觀。
如何在 C# 中讀取條碼?
要在 C# 中讀取條碼,使用 IronBarcode 庫的 BarcodeReader.Read 方法。此方法處理包含條碼的圖像文件並返回解碼值以供進一步處理。
使用 IronBarcode 庫進行條碼生成的優勢是什麼?
IronBarcode 庫提供了用戶友好的 API,支援各種條碼編碼,提供自動校驗和計算,並允許將圖像導出為多種格式,使其成為條碼生成和自定義的靈活高效工具。
IronBarcode 可以導出為哪些圖像格式?
IronBarcode 可以將條碼圖像導出為多種格式,包括 JPEG,這允許靈活地處理和集成條碼圖像到不同的應用程式中。
在購買之前可以試用 IronBarcode 嗎?
是的,您可以試用 IronBarcode 的免費試用版,充分探索其潛力和功能,然後再選擇商業許可證,確保您能夠在 C# 專案中有效地集成條碼功能。

