使用 IRONBARCODE 如何在 C# 中產生 Code 128 條碼 Curtis Chau 更新:2025年7月28日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 條碼在現代商業營運中至關重要,從庫存管理到產品標籤和運輸都離不開它。 在各種條碼代碼集中, Code 128因其多功能性和廣泛應用而脫穎而出。 在本文中,我們將探討如何使用IronBarcode庫在 C# 中建立 Code 128 條碼產生器。 如何在 C# 中產生 Code 128 條碼 安裝 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,用於調整內容、大小和外觀。 其解碼功能、自動校驗和計算以及影像匯出功能使其成為庫存管理及其他領域開發人員的寶貴工具。 該庫對屬性、邊距、字體和顏色的自訂選項增強了其在條碼相關任務中的多功能性。 用 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。 在頂部選單中,前往"檢視">"其他視窗">"套件管理員控制台"以開啟套件管理員控制台。 在軟體套件管理器控制台中,您可以使用Install-Package指令安裝 IronBarcode 軟體套件。 輸入以下指令並按下回車鍵: Install-Package BarCode Install-Package BarCode SHELL 此指令會將最新版本的 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 條碼? 若要在 C# 中產生 Code 128 條碼,請使用 IronBarcode 庫中的BarcodeWriter.CreateBarcode方法,並傳入所需的輸入字串,同時指定 Code 128 作為編碼類型。之後,您可以使用SaveAsJpeg等方法匯出產生的條碼影像。 代碼 128 的用途是什麼? Code 128 用於以緊湊的條碼格式編碼字母數字資料和特殊字元。由於其資料密度高且用途廣泛,因此非常適合庫存管理、產品標籤和運輸應用。 我可以使用 IronBarcode 自訂條碼的外觀嗎? 是的,您可以使用 IronBarcode 自訂條碼的外觀,透過ChangeBackgroundColor和ChangeBarCodeColor修改其顏色,並透過ResizeTo方法調整其大小。 如何在C#中讀取條碼? 若要在 C# 中讀取條碼,請使用 IronBarcode 庫中的BarcodeReader.Read方法。此方法會處理包含條碼的影像文件,並傳回解碼後的值以便進一步處理。 使用 IronBarcode 庫產生條碼有哪些優點? IronBarcode 庫提供使用者友善的 API,支援各種條碼編碼,提供自動校驗和運算,並允許以多種格式匯出影像,使其成為條碼產生和自訂的靈活高效的工具。 IronBarcode可以匯出哪些影像格式? IronBarcode 可以將條碼影像匯出為各種格式,包括 JPEG,從而可以靈活地處理條碼影像並將其整合到不同的應用程式中。 購買前可以試用IronBarcode嗎? 是的,您可以先試用 IronBarcode 的免費試用版,探索其全部潛力和功能,然後再決定是否購買商業許可證,確保您能夠有效地將條碼功能整合到您的 C# 專案中。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2026年1月22日 ASP.NET 條碼掃描器教學:C# 條碼產生器指南 學習如何使用 IronBarcode 在 ASP.NET 中掃描條碼 閱讀更多 發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多 發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多 如何在VB.NET讀取攝影機拍攝的條碼如何在 C# 中建立 Code 39 條...
發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多
發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多