如何在 C# 中使用 OcrProgress Tracking

Customize and Style Barcodes in C# for .NET with IronBarcode.

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 可讓開發人員在 C# 中透過改變顏色、調整尺寸和新增註解來自訂條碼,並透過簡單的方法呼叫,如 ChangeBarCodeColor()ResizeTo() 來進行完整的樣式控制。

多年來,Barcode 的使用越來越普及,無論是儲存資料、ID 或網頁 URL,都被廣泛使用。 在某些應用程式中,產品上的 BarCode 是可見的,因此對造型選項的需求也隨之增加。 因此,某些 BarCode 類型已發展出獨特的外觀,例如 PDF417AztecIntelligentMailMaxiCodeDataMatrix 等等。 如需支援格式的完整清單,請參閱我們的 Supported BarCode Formats 文件。

此外,IronBarcode 還為用戶提供了進一步調整條碼風格的選項,包括條碼顏色條碼大小調整背景顏色等方面。 透過我們的開源函式庫IronDrawing ,這一切才得以實現。 這些造型功能建立在 IronBarcode 的 綜合條碼生成功能之上。

快速入門:自訂條碼顏色和背景

下面是一個簡單的示例,展示了開發人員如何使用 IronBarcode 對條碼的條和背景快速應用自定義顏色。 您會發現只需一次連鎖呼叫,就能輕鬆產生樣式化的 BarCode。 如需更多進階範例,請查看我們的 C# BarCode Image Generator 教程

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

    IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128)
        .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue)
        .ChangeBackgroundColor(IronSoftware.Drawing.Color.White)
        .SaveAsImage("styled.png");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

如何調整 BarCode 的大小?

何時應該使用 ResizeTo 方法? 調整條碼大小是使用者可以使用 IronBarcode 實現的自訂功能之一。 要使用此功能,只需呼叫`ResizeTo`方法,並以像素 (px) 為單位輸入條碼的新寬度和高度測量值即可。 此動作會觸發無損地重新渲染 BarCode。 此方法可在調整條碼尺寸的同時保持條碼的品質,非常適合需要將條碼放入特定版面或列印尺寸的場合。
請注意條碼無法讀取的過小值將被忽略。
```csharp using IronBarCode; public class BarcodeResizer { public static void ResizeBarcode(string barcodeText, int newWidth, int newHeight) { // Generate a barcode BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128) // Resize the barcode .ResizeTo(newWidth, newHeight) // Save the resized barcode .SaveAsImage("resized_barcode.png"); } // Example usage with different size requirements public static void ResizeForDifferentFormats() { var barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128); // Resize for product label barcode.ResizeTo(200, 50).SaveAsImage("product_label.png"); // Resize for shipping label barcode.ResizeTo(300, 75).SaveAsImage("shipping_label.png"); // Resize for inventory tag barcode.ResizeTo(150, 40).SaveAsImage("inventory_tag.png"); } } ``` 可以對`GeneratedBarcode`物件呼叫`ResizeTo`方法。 在處理不同的輸出格式時,您可能也想探索我們的 [Create BarCode as PDF](https://ironsoftware.com/csharp/barcode/how-to/create-barcode-as-pdf/) 指南。以下是執行上述程式碼片段所產生的 BarCode 影像。
Original barcode with standard dimensions before resize operation
Resized barcode showing clear black and white vertical bars after dimension modification

為何使用 ResizeToMil 方法處理一維 BarCode? IronBarcode 中提供的另一個調整大小的方法是`ResizeToMil`方法。 與`ResizeTo`方法不同,此方法會調整以下元件: -**條碼元素:**條碼最窄元素的寬度,以千分之一英吋(mil)為單位測量。 -**高度:**條碼的高度,以英吋為單位測量(預設值為 1 英吋)。 -**解析度:**每英吋點數(預設值為 96 DPI)。 此方法特別適用於 1D BarCode,常用於精確測量非常重要的工業應用。 mil 測量系統是業界標準,可確保不同掃描器和列印條件下的 BarCode 可讀性一致。 ```csharp using IronBarCode; public class BarcodeResizer { public static void ResizeBarcodeToMil(string barcodeText, int elementWidthMil, int heightInches, int dpi = 96) { // Generate a barcode BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128) // Resize the barcode to mil .ResizeToMil(elementWidthMil, heightInches, dpi) // Save the resized barcode .SaveAsImage("resized_barcode_mil.png"); } // Example for different industrial standards public static void CreateIndustrialBarcodes() { // Standard retail barcode (10 mil width, 1 inch height) BarcodeWriter.CreateBarcode("RETAIL-001", BarcodeEncoding.Code128) .ResizeToMil(10, 1, 300) .SaveAsImage("retail_barcode.png"); // High-density warehouse barcode (5 mil width, 0.5 inch height) BarcodeWriter.CreateBarcode("WAREHOUSE-002", BarcodeEncoding.Code128) .ResizeToMil(5, 0.5f, 600) .SaveAsImage("warehouse_barcode.png"); // Large shipping barcode (15 mil width, 2 inch height) BarcodeWriter.CreateBarcode("SHIP-003", BarcodeEncoding.Code128) .ResizeToMil(15, 2, 200) .SaveAsImage("shipping_barcode.png"); } } ``` 您也可以對`GeneratedBarcode`物件呼叫此方法。 有關設定精確條碼尺寸的詳細資訊,請參閱我們的 [Set Barcodes Margins](https://ironsoftware.com/csharp/barcode/how-to/setting-margins-barcode/) 指南。在下面的圖片中,您將看到應用 `ResizeToMil` 方法的效果:條碼邊緣的空白被消除,條碼的最窄元素和高度都根據提供給該方法的參數值進行調整。
Original barcode with standard dimensions before ResizeToMil method is applied
Linear barcode showing result after ResizeToMil method application with vertical black and white bars

如何變更 BarCode 和背景顏色? 條碼樣式設計中最受歡迎的功能之一是能夠同時變更條碼和背景顏色。 得益於[IronDrawing](/open-source/csharp/drawing/docs/) ,IronBarcode 具備了這種功能。 透過使用`GeneratedBarcode`物件上的`ChangeBarCodeColor`和`ChangeBackgroundColor`方法,使用者可以變更條碼及其背景的顏色。 此功能對於品牌塑造或為特殊活動或產品線建立主題 BarCode 時特別有用。 ```csharp using IronBarCode; using IronSoftware.Drawing; // Required for color manipulation public class BarcodeColorChanger { public static void ChangeBarcodeColors(string barcodeText, Color barcodeColor, Color backgroundColor) { // Generate a barcode var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128); // Change the barcode color barcode.ChangeBarCodeColor(barcodeColor); // Change the background color barcode.ChangeBackgroundColor(backgroundColor); // Save the colored barcode barcode.SaveAsImage("colored_barcode.png"); } // Example: Create branded barcodes with company colors public static void CreateBrandedBarcodes() { // Company brand colors example var barcode = BarcodeWriter.CreateBarcode("BRAND-2024", BarcodeEncoding.Code128); // Apply brand colors barcode.ChangeBarCodeColor(Color.FromHex("#1E3A8A")) // Company blue .ChangeBackgroundColor(Color.FromHex("#F3F4F6")) // Light gray background .SaveAsImage("branded_barcode.png"); // Create seasonal variation var seasonalBarcode = BarcodeWriter.CreateBarcode("HOLIDAY-2024", BarcodeEncoding.Code128); seasonalBarcode.ChangeBarCodeColor(Color.DarkGreen) .ChangeBackgroundColor(Color.LightYellow) .SaveAsImage("seasonal_barcode.png"); } } ``` 在處理彩色 BarCode 時,必須在條碼與背景顏色之間維持足夠的對比度,以確保可讀性。 如需更多 QR 代碼特有的樣式選項,請參閱我們的 [ 自訂和樣式 QR 代碼](https://ironsoftware.com/csharp/barcode/how-to/customize-qr-code-style/)教學。
自訂綠色背景和棕褐色前景顏色的 QR 代碼,展示條碼顏色自訂

如何在 BarCode 上新增註解? IronBarcode 也提供了新增和設定條碼註解樣式的選項。 [IronDrawing](/open-source/csharp/drawing/docs/)的功能也有助於註釋的樣式設置,包括編輯註釋顏色和字體。 註解對於在機器可讀的條碼旁提供人類可讀的資訊非常重要,因此對於庫存管理、產品標籤和運送應用來說至關重要。 ```csharp using IronBarCode; using IronSoftware.Drawing; // Required for font and color manipulation public class BarcodeAnnotator { public static void AnnotateBarcode(string barcodeText, string annotationText, Font annotationFont, Color annotationColor, float annotationSpacing) { // Generate a barcode var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128); // Add annotation above the barcode barcode.AddAnnotationTextAboveBarcode(annotationText, annotationFont, annotationColor, annotationSpacing); // Add barcode value text below the barcode barcode.AddBarcodeValueTextBelowBarcode(annotationFont, annotationColor, annotationSpacing); // Save the annotated barcode barcode.SaveAsImage("annotated_barcode.png"); } // Example: Create product label with annotations public static void CreateProductLabel() { var productCode = "PRD-12345-XL"; var barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128); // Define fonts for different purposes var titleFont = new Font("Arial", FontStyle.Bold, 14); var valueFont = new Font("Arial", FontStyle.Regular, 12); // Add product name above barcode.AddAnnotationTextAboveBarcode("Premium Widget XL", titleFont, Color.Black, 5); // Add product code below barcode.AddBarcodeValueTextBelowBarcode(valueFont, Color.DarkGray, 3); // Apply additional styling barcode.ResizeTo(250, 80) .SaveAsImage("product_label_annotated.png"); } } ```
由 IronBarcode 生成的茶色和米色 QR 代碼,包含 ironsoftware.com URL
作為前面程式碼片段的擴展,我們實例化了兩個新的`IronSoftware.Drawing.Font`對象,用作條碼上方和下方註釋的字體。 只有 **Font Family** 是實體化字型的必要條件,不過您可以指定其他屬性,例如大小和樣式,以獲得更多控制權。 - `AddAnnotationTextAboveBarcode` :在條碼上方新增自訂註解文字。 - `AddBarcodeValueTextBelowBarcode` :在條碼下方新增條碼值。 這兩種方法接受相同的參數: `IronSoftware.Drawing.Font`物件、 `IronSoftware.Drawing.Color`物件以及條碼和文字之間的間距。 此外, `AddAnnotationTextAboveBarcode`方法需要一個字串作為註釋文本,因為它會在條碼上方添加自訂文字。 IronBarcode 提供廣泛的客製化選項來設定條碼的樣式。 對於需要在註解中支援 Unicode 的應用程式,請查看我們的 [Writing Unicode BarCode](https://ironsoftware.com/csharp/barcode/how-to/writing-in-unicode/) 指南。若要瞭解自訂 QR 碼的更多資訊,請參閱"[如何自訂及新增標誌至 QR 碼](/csharp/barcode/how-to/customize-qr-code-style/)"。 若要將風格化的 BarCode 匯出為不同格式,請探索我們的 [Create Barcode as HTML](https://ironsoftware.com/csharp/barcode/how-to/create-barcode-as-html/) 教學。

常見問題解答

如何在 C# 中變更 BarCode 的顏色?

IronBarcode 提供了 ChangeBarCodeColor() 方法來輕鬆自訂條碼顏色。只需在創建您的 BarCode 後鏈結此方法,即可應用 IronSoftware.Drawing.Color 調色板中的任何顏色,從而完全控制您的條形碼的視覺外觀。

我應該使用什麼方法來調整 BarCode 的大小而不降低品質?

使用 IronBarcode 的 ResizeTo() 方法來調整條碼大小而不會造成品質損失。此方法可在指定的寬度和高度(以像素為單位)下觸發條碼的無損重新渲染,在保持清晰度的同時調整尺寸以適應特定的佈局或打印要求。

我可以自訂 BarCode 的背景顏色嗎?

是的,IronBarcode 允許您使用 ChangeBackgroundColor() 方法自定義條碼背景。此功能可讓您使用 IronSoftware.Drawing.Color 調色板設定任何背景顏色,使其與您的設計需求無縫整合。

哪些 BarCode 格式支援獨特的樣式選項?

IronBarcode 支援各種具有獨特外觀的條碼格式,包括 PDF417、Aztec、IntelligentMail、MaxiCode 和 DataMatrix。每種格式都有其獨特的視覺特徵,同時仍允許通過 IronBarcode 的樣式化方法進行額外的定制。

如何在 BarCode 上新增註解?

IronBarcode 可讓您在 BarCode 的上方和下方新增註解,增強可讀性,並提供額外的上下文。此功能對於在條碼旁邊加入人類可讀的文字、產品代碼或其他識別資訊特別有用。

ResizeTo 和 ResizeToMil 方法有何不同?

IronBarcode 提供兩種調整大小的方法:ResizeTo()用於以像素為基礎的大小調整和無損失的重新渲染,ResizeToMil()用於使用密爾量來調整條碼元素的大小。這兩種方法在滿足不同量測需求的同時,都能保持品質。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,002,059 | 版本: 2025.12 剛發表