如何在 C# 中使用 OcrProgress Tracking

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

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

多年來,條碼的使用越來越普及,並被廣泛應用於各種領域,無論是儲存資料、ID 或網頁 URL。 在某些應用程式中,條碼會顯示在產品上,這導致對條碼樣式選項的需求增加。 因此,一些條碼類型/編碼出現了其獨特的外觀,例如PDF417AztecIntelligentMailMaxiCodeDataMatrix等等。

除此之外,IronBarcode 也為使用者提供了進一步自訂條碼樣式的選項,包括條碼顏色條碼大小調整背景顏色等。 透過我們的開源函式庫IronDrawing ,這一切才得以實現。

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

這裡有一個簡單的範例,展示了開發人員如何使用 IronBarcode 快速地為條碼的條形和背景套用自訂顏色。 你會發現,只需一次鍊式調用,就能輕鬆產生樣式化的條碼。

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

條碼調整大小範例

使用 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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcode(ByVal barcodeText As String, ByVal newWidth As Integer, ByVal newHeight As Integer)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeTo(newWidth, newHeight).SaveAsImage("resized_barcode.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

可以對GeneratedBarcode物件呼叫ResizeTo方法。 下面顯示的是執行上述程式碼片段所產生的條碼影像。

Barcode before resize
Barcode after resize

使用 ResizeToMil 方法

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

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

這種方法特別適用於一維條碼。

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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcodeToMil(ByVal barcodeText As String, ByVal elementWidthMil As Integer, ByVal heightInches As Integer, Optional ByVal dpi As Integer = 96)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeToMil(elementWidthMil, heightInches, dpi).SaveAsImage("resized_barcode_mil.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

您也可以對GeneratedBarcode物件呼叫此方法。 在下面的圖片中,您將看到應用ResizeToMil方法的效果:條碼邊緣的空白被消除,條碼的最窄元素和高度都根據提供給該方法的參數值進行調整。

Barcode before ResizeToMil
Barcode after ResizeToMil

更改條碼和背景顏色

條碼樣式設計中最受歡迎的功能之一是能夠同時變更條碼和背景顏色。 得益於IronDrawing ,IronBarcode 具備了這種功能。 透過使用GeneratedBarcode物件上的ChangeBarCodeColorChangeBackgroundColor方法,使用者可以變更條碼及其背景的顏色。 下面這段簡單的程式碼片段示範如何實現這一點。

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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for color manipulation

Public Class BarcodeColorChanger
	Public Shared Sub ChangeBarcodeColors(ByVal barcodeText As String, ByVal barcodeColor As Color, ByVal backgroundColor As Color)
		' Generate a barcode
		Dim 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")
	End Sub
End Class
$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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for font and color manipulation

Public Class BarcodeAnnotator
	Public Shared Sub AnnotateBarcode(ByVal barcodeText As String, ByVal annotationText As String, ByVal annotationFont As Font, ByVal annotationColor As Color, ByVal annotationSpacing As Single)
		' Generate a barcode
		Dim 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")
	End Sub
End Class
$vbLabelText   $csharpLabel
附註解的彩色條碼

作為前面程式碼片段的擴展,我們實例化了兩個新的IronSoftware.Drawing.Font對象,用作條碼上方和下方註釋的字體。 只需字體系列即可實例化字體。

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

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

IronBarcode 為用戶提供了多種自訂和設計條碼的機會,唯一的限制就是用戶的想像。 要了解有關自訂二維碼的更多信息,請參閱"如何自訂二維碼並添加徽標"。

常見問題解答

如何在 .NET C# 中更改 BarCode 的顏色?

您可以使用 IronBarcode for .NET 的 ChangeBarCodeColor 方法在 .NET C# 中更改条形码的颜色。這可讓您自訂條碼的外觀以符合您的設計需求。

在 .NET C# 中,有哪些方法可用於調整 BarCode 的大小?

IronBarcode 提供了像 ResizeTo 這樣的方法,用於調整條碼大小(以像素為單位),以及 ResizeToMil 這樣的方法,用於調整寬度(以千分之一英寸為單位),適用於 1D 條碼。

我可以在 C# 中為 BarCode 加入文字註解嗎?

是的,您可以使用 IronBarcode 的 AddAnnotationTextAboveBarcodeAddBarcodeValueTextBelowBarcode 方法在 C# 中為條碼添加文字註釋,從而增強標籤和資訊。

如果我的已調整大小的 BarCode 無法讀取,該怎麼辦?

如果您重新調整大小的條碼因尺寸過小而無法讀取,IronBarcode 將忽略這些值以維持可讀性,確保條碼維持功能。

如何在 .NET C# 中確保高品質的 BarCode 渲染?

使用 IronBarcode 的無損大小調整方法,如 ResizeToResizeToMil 可實現高品質的條碼渲染,在調整大小的同時保持圖像品質。

什麼函式庫可以協助條碼顏色和注解樣式化?

IronDrawing 是一個開放原始碼的函式庫,可協助顏色處理和注解樣式,讓 IronBarcode 的條碼設計更具創意和個人化。

是否可以在 C# 中變更 BarCode 註解的字型?

是的,您可以使用 IronSoftware.Drawing.Font 物件自訂 BarCode 註解的字型,提供條碼上方和下方文字樣式的彈性。

我在哪裡可以下載 C# 自訂 BarCode 的函式庫?

用於自定義條碼的 C# 函式庫可從 NuGet 下載,網址為 https://www.nuget.org/packages/BarCode/,使您能夠開始使用 IronBarcode 自定義條碼。

如何在 .NET C# 中自訂 BarCode 的背景顏色?

您可以使用 IronBarcode 的 ChangeBackgroundColor 方法在 .NET C# 中定制條形碼的背景顏色,從而獲得獨特的設計和品牌機會。

如何在調整大小後保持 BarCode 的可讀性?

為了在調整尺寸後保持可讀性,請使用 IronBarcode 的調整尺寸方法,該方法可確保條碼尺寸不會影響其功能性和清晰度。

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