跳過到頁腳內容
使用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 程式庫,可簡化條碼的產生、解碼和自訂。 支援各種條碼編碼,如 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 套件,您可以依照下列步驟操作:

1.開啟 Visual Studio。

  1. 在頂部選單中,前往"檢視">"其他視窗">"套件管理員控制台"以開啟套件管理員控制台。
  2. 在軟體套件管理器控制台中,您可以使用Install-Package指令安裝 IronBarcode 軟體套件。 輸入以下指令並按下回車鍵:

    Install-Package BarCode
    Install-Package BarCode
    SHELL
  3. 此指令會將最新版本的 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(聯合影像專家小組)。

輸出

產生的條碼如下:

如何在 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

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

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

產生和保存條碼的程式碼是相同的。 我剛剛添加了兩行程式碼,用於更改背景色和條碼顏色。 解釋如下:

  • ChangeBackgroundColor:myBarcode物件呼叫ChangeBackgroundColor方法。 此方法會改變條碼影像的背景顏色。 傳遞給ChangeBackgroundColor參數是IronSoftware.Drawing.Color.Cornsilk ,它指定所需的背景顏色。 在這種情況下,背景顏色設定為玉米須色,這是一種淡黃色。

  • ChangeBarCodeColor:myBarcode物件呼叫ChangeBarCodeColor方法。 這種方法會改變條碼的顏色。 傳遞給ChangeBarCodeColor參數是IronSoftware.Drawing.Color.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 - 讀取條碼的控制台輸出

結論

總之,本文示範如何使用IronBarcode庫在 C# 中建立一個 Code 128 條碼產生器。 透過利用 IronBarcode 的功能,開發人員可以輕鬆產生自訂設計Code 128 條碼樣式,用於各種應用,包括庫存管理、產品標籤和運輸。 透過遵循本教程,開發人員可以將強大的條碼功能整合到他們的 C# 專案中,從而提高處理與條碼產生和解碼相關的任務的效率。 IronBarcode 的多功能性和直覺的 API 使其成為開發人員開發涉及條碼實現的應用程式的寶貴工具。

在使用 IronBarcode 庫產生 Code 128 條碼的過程中,開發人員可以靈活地自訂代碼集選擇字符,從而確保對具有不同 ASCII 值的資料進行最佳編碼。 渲染程式碼可無縫適應首選影像格式,讓開發人員可以選擇將條碼儲存為 JPEG 或其他格式。 此外,新增停止字元可確保產生的條碼中編碼資訊的準確終止。

IronBarcode 提供免費試用,以充分發揮該庫的潛力,滿足其開發需求。 這種方法使開發人員能夠在購買商業許可證之前評估 IronBarcode 的功能。

常見問題解答

如何在 C# 中生成 Code 128 BarCode?

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

Code 128 用來做什麼?

Code 128 用於以精簡的 BarCode 格式編碼字母數字資料和特殊字符。由於其高資料密度和多功能性,是庫存管理、產品標籤和運送應用的理想選擇。

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

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

如何在 C# 中讀取 BarCode?

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

使用 IronBarcode library 生成条码有哪些优势?

IronBarcode 函式庫提供使用者友善的 API、支援多種條碼編碼、提供自動校驗和計算,並允許匯出多種格式的影像,使其成為靈活且有效率的條碼產生與客製化工具。

IronBarcode 可以輸出哪些圖像格式?

IronBarcode 可以將條碼影像匯出成多種格式,包括 JPEG,因此可以靈活處理條碼影像並將其整合到不同的應用程式中。

是否可以在購買前試用 IronBarcode?

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

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