IronBarcode 操作指南 自訂和樣式化條碼 How to Customize and Style Barcodes Hairil Hasyimi Bin Omar 更新日期:7月 22, 2025 Download IronBarcode NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 多年來,條碼的使用日益普及,並且用於廣泛的應用,包括存儲數據、ID或網頁的URL。 在某些應用中,條碼會顯示在產品上,這導致了對條碼設計選項的需求增加。 因此,一些條碼類型/編碼具有了它們獨特的外觀,如 PDF417、Aztec、IntelligentMail、MaxiCode、DataMatrix 等等。 除此之外, IronBarcode 提供用戶進一步設計條碼的選項,包括 條碼顏色、條碼調整大小 以及 背景顏色。 這一切都由我們的開源庫 IronDrawing 提供支持。 快速開始:自訂條碼顏色和背景 這裡有一個簡單的例子,顯示開發人員如何使用 IronBarcode 快速將自訂顏色應用到條碼的條和背景。 你將看到只需一次鏈式調用就可以生成一個設計好的條碼是多麼簡單。 Get started making PDFs with NuGet now: Install IronBarcode with NuGet Package Manager PM > Install-Package BarCode Copy and run this code snippet. IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128) .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue) .ChangeBackgroundColor(IronSoftware.Drawing.Color.White) .SaveAsImage("styled.png"); Deploy to test on your live environment Start using IronBarcode in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小工作流程(5步) 下載C#庫來自訂和設計條碼 使用 ResizeTo 方法觸發無失真的重新渲染 使用 ResizeToMil 方法調整條碼元素的大小 改變條碼和背景的顏色 在條碼的上方和下方添加條碼註釋 調整條碼大小示例 使用 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 ResizeTo 方法可以在 GeneratedBarcode 對象上調用。 下面是運行上述代碼片段生成的條碼圖片。 class="content-img-align-center"> class="center-image-wrapper"> 調整大小前 class="content-img-align-center"> class="center-image-wrapper"> 調整大小後 使用 ResizeToMil 方法 IronBarcode 中可用的調整大小的另一個方面是 ResizeToMil 方法。 與 ResizeTo 方法不同,這個方法會調整以下組件: 條碼元素:最窄的條碼元素的寬度,以千分之一英寸(mil)為單位。 高度:條碼的高度,以英寸為單位(默認為1英寸)。 解析度:每英寸的點數(默認為96 DPI)。 此方法特別適合於1D條碼。 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 方法後的效果:條碼邊緣的白色空間被消除,而最窄的元素和條碼的高度根據提供給方法的參數值進行了調整。 class="content-img-align-center"> class="center-image-wrapper"> ResizeToMil 前 class="content-img-align-center"> class="center-image-wrapper"> ResizeToMil 後 更改條碼和背景顏色 設計條碼的一個最受歡迎的功能是能夠更改條碼和背景顏色。 得益於 IronDrawing, IronBarcode 提供了這種能力。 通過在 GeneratedBarcode 對象上使用 ChangeBarCodeColor 和 ChangeBackgroundColor 方法,用戶可以更改條碼及其背景的顏色。 下面是一個簡單的代碼片段展示如何實現這一點。 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 class="content-img-align-center"> class="center-image-wrapper"> 添加條碼註釋示例 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 class="content-img-align-center"> 作為上一個代碼片段的擴展,我們創建了兩個新的 IronSoftware.Drawing.Font 對象作為條碼上方和下方註釋的字體。 僅需要 字體系列 來創建字體。 AddAnnotationTextAboveBarcode:在條碼上方添加自訂註釋文本。 AddBarcodeValueTextBelowBarcode:在條碼下方添加條碼值。 這兩個方法都接受相同的參數:IronSoftware.Drawing.Font 對象、IronSoftware.Drawing.Color 對象以及條碼和文本之間的間距量。 另外,AddAnnotationTextAboveBarcode 方法需要一個字符串作為註釋文本,因為它在條碼上方添加自訂文本。 IronBarcode 為用戶提供了廣泛的機會來自訂和設計他們的條碼,唯想像力限制。 要了解更多關於自訂 QR 碼的信息,請參閱"如何自訂和添加徽標到QR碼"。 常見問題解答 如何在 .NET C# 中更改條碼顏色? 您可以使用 IronBarcode 的 ChangeBarCodeColor 方法更改 .NET C# 中條碼的顏色。這允許您定制條碼的外觀以符合您的設計需求。 在 .NET C# 中,有哪些方法可以調整條碼大小? IronBarcode 提供如 ResizeTo 方法用于按像素調整條碼大小,以及 ResizeToMil 方法用于調整 1D 條碼的寬度,單位為千分之一英吋。 我可以在 C# 中向條碼添加文本註釋嗎? 可以,您可以使用 IronBarcode 的 AddAnnotationTextAboveBarcode 和 AddBarcodeValueTextBelowBarcode 方法在 C# 中為條碼添加文本註釋,從而增強標籤和信息。 如果我調整過大小的條碼無法讀取,我該怎麼辦? 如果調整過大小的條碼由於尺寸過小而無法讀取,IronBarcode 會忽略那些值以維持可讀性,確保條碼繼續正常運作。 如何在 .NET C# 中確保高質量的條碼渲染? 通過使用 IronBarcode 的無損調整大小方法(例如 ResizeTo 和 ResizeToMil),可以實現高質量的條碼渲染,這些方法在調整大小時保持圖像質量。 哪個庫可以協助條碼的顏色和註釋樣式? IronDrawing,一個開源庫,能夠協助進行顏色操作和註釋樣式,允許在 IronBarcode 中創造性和個性化的條碼設計。 在 C# 中可以更改條碼註釋的字體嗎? 可以,您可以使用 IronSoftware.Drawing.Font 對象來定制條碼註釋的字體,提供文本樣式的靈活性。 從哪裡可以下載用於 C# 中條碼自訂的庫? 可以從 NuGet(https://www.nuget.org/packages/BarCode/)下載 C# 條碼自訂庫,這使您可以開始使用 IronBarcode 自訂條碼。 如何在 .NET C# 中自訂條碼的背景顏色? 您可以使用 IronBarcode 的 ChangeBackgroundColor 方法在 .NET C# 中自訂條碼的背景顏色,為獨特的設計和品牌機會提供可能。 調整大小後保持條碼可讀性的最佳方式是什麼? 為保持調整大小後的可讀性,使用 IronBarcode 的調整大小方法,確保條碼的尺寸不會影響其功能性和清晰性。 Hairil Hasyimi Bin Omar 立即與工程團隊聊天 軟體工程師 和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。 準備好開始了嗎? Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,935,276 查看許可證