如何在 C# 中使用 OcrProgress Tracking

使用IronBarcode在 C# for .NET中自訂條碼樣式

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

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

多年來,條碼的使用越來越普及,並被廣泛應用於各種領域,無論是儲存資料、ID 或網頁 URL。 在某些應用場景中,條碼會印在產品上,導致對樣式選擇的需求增加。 因此,一些條碼類型發展出了獨特的外觀,例如 DataMatrix 等等。 有關支援的格式的完整列表,請參閱我們的"支援的條碼格式"文件。

此外, IronBarcode也為使用者提供了進一步設定條碼樣式的選項,包括條碼顏色條碼調整大小背景顏色等。 透過我們的開源函式庫IronDrawing ,這一切才得以實現。 這些樣式功能建立在 IronBarcode全面的條碼產生功能之上。

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

這裡有一個簡單的範例,展示了開發人員如何使用IronBarcode快速地為條碼的條形和背景應用自訂顏色。 你會發現,只需一次鍊式調用,就能輕鬆產生樣式化的條碼。 如需更多進階範例,請查看我們的C# 條碼影像產生器教學

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    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

如何調整條碼大小?

何時應該使用 ResizeTo 方法?

調整條碼大小是使用者可以使用IronBarcode實現的自訂功能之一。 要使用此功能,只需呼叫 ResizeTo 方法,並以像素 (px) 為單位輸入條碼的新寬度和高度測量值。 此操作會觸發條碼的無損重新渲染。 這種方法在調整條碼尺寸的同時保持條碼的質量,因此非常適合需要將條碼放入特定佈局或列印尺寸的場景。

請注意條碼無法讀取的數值將被忽略。

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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

可以在 GeneratedBarcode 物件上呼叫 ResizeTo 方法。 使用不同的輸出格式時,您可能還需要參考我們的"建立條碼為 PDF"指南。以下是運行上述程式碼片段所產生的條碼圖像。

Original barcode with standard dimensions before resize operation
Resized barcode showing clear black and white vertical bars after dimension modification

為什麼一維條碼要使用 ResizeToMil 方法?

IronBarcode中提供的另一個調整大小的方面是 ResizeToMil 方法。 與 ResizeTo 方法不同,此方法調整以下元件:

-條碼元素:條碼最窄元素的寬度,以千分之一英吋(mil)為單位測量。 -高度:條碼的高度,以英吋為單位測量(預設值為 1 英吋)。 -解析度:每英吋點數(預設值為 96 DPI)。

這種方法特別適用於一維條碼,常用於對測量精度要求極高的工業應用。 mil 測量系統是業界標準,可確保條碼在不同的掃描器和列印條件下具有一致的可讀性。

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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

你也可以在 GeneratedBarcode 物件上呼叫此方法。 如需了解有關設定精確條碼尺寸的更多信息,請參閱我們的"設定條碼邊距"指南。在下圖中,您可以看到應用 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

如何更改條碼和背景顏色?

條碼樣式設計中最受歡迎的功能之一是能夠同時變更條碼和背景顏色。 得益於IronDrawing , IronBarcode具備了這種功能。 透過使用 ChangeBarCodeColorChangeBackgroundColor 方法,使用者可以在 GeneratedBarcode 物件上變更條碼及其背景的顏色。 此功能在品牌推廣或為特殊活動或產品線建立主題條碼時特別有用。

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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

使用彩色條碼時,保持條碼和背景顏色之間有足夠的對比度非常重要,以確保可讀性。 有關二維碼的更多樣式選項,請參閱我們的"自訂和樣式二維碼"教學。

帶有自訂綠色背景和棕褐色前景的二維碼,展示了條碼顏色自訂功能。

如何為條碼添加註解?

IronBarcode也提供了新增和設定條碼註解樣式的選項。 IronDrawing的功能也有助於註釋的樣式設置,包括編輯註釋顏色和字體。 註釋對於在機器可讀條碼旁邊提供人可讀資訊至關重要,因此對於庫存管理、產品標籤和運輸應用至關重要。

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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel
由IronBarcode產生的青色和米色二維碼,包含 ironsoftware.com 網址

作為前面程式碼片段的擴展,我們實例化了兩個新的 IronSoftware.Drawing.Font 對象,用作條碼上方和下方註釋的字體。 只需指定字體系列即可實例化字體,但您也可以指定大小和样式等其他屬性以進行更多控制。

  • AddAnnotationTextAboveBarcode: 在條碼上方新增自訂註解文字。
  • AddBarcodeValueTextBelowBarcode: 在條碼下方新增條碼值。

這兩種方法接受相同的參數:IronSoftware.Drawing.Font 物件、IronSoftware.Drawing.Color 物件以及條碼和文字之間的間距。 此外,AddAnnotationTextAboveBarcode 方法需要一個字串作為註釋文本,因為它會在條碼上方添加自訂文字。

IronBarcode提供多種條碼樣式自訂選項。 對於需要在註解中支援 Unicode 的應用,請參閱我們的《編寫 Unicode 條碼》指南。要了解有關自訂二維碼的更多信息,請參閱"如何自訂二維碼並添加徽標"。 若要將樣式化的條碼匯出為不同格式,請參閱我們的"建立條碼為 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,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。