IronBarcode 操作指南 自定义和样式化条形码 使用 IronBarcode 在 C# for .NET 中自定义条形码样式 Hairil Hasyimi Bin Omar 已更新:七月 22, 2025 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 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快速将自定义颜色应用于条形码的条形和背景。 你会看到仅通过一次链式调用就能生成风格化的条形码是多么容易。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronBarcode PM > Install-Package BarCode 复制并运行这段代码。 IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128) .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue) .ChangeBackgroundColor(IronSoftware.Drawing.Color.White) .SaveAsImage("styled.png"); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronBarcode,免费试用! 免费试用30天 最小工作流程(5 个步骤) 下载 C# 库以自定义条形码样式 使用ResizeTo方法触发无损重新渲染 使用ResizeToMil方法调整条形码元素的大小 更改条形码及其背景的颜色 在条形码上方和下方添加条形码注释 调整条形码大小示例 使用ResizeTo方法 调整条形码大小是用户可以使用IronBarcode实现的一个自定义方面。 要使用此功能,只需调用ResizeTo方法并输入条形码的新宽度和高度测量(以像素为单位)。 此操作将触发条形码的无损重新渲染。 请注意条形码无法读取的过小值将被忽略。 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对象上调用。 以下是通过运行上面的代码片段生成的条形码图像。 调整大小之前 调整大小后 使用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方法的效果:条形码边缘的空白被消除,并且根据提供给方法的参数值调整了最窄元素和条形码的高度。 在调整大小到千兆之前 调整大小到千兆后 更改条形码和背景颜色 条形码样式最受欢迎的功能之一是可更改条形码和背景颜色。 得益于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 添加条形码注释示例 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 作为前面代码片段的扩展,我们实例化了两个新的Iron Software.Drawing.Font对象,用作条形码上方和下方注释的字体。 仅需要字体系列来实例化字体。 AddAnnotationTextAboveBarcode:在条形码上方添加自定义注释文本。 AddBarcodeValueTextBelowBarcode:在条形码下方添加条形码值。 这两个方法都接受相同的参数:Iron Software.Drawing.Font对象,一个Iron Software.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#库? 用于条形码自定义的C#库可以从NuGet下载,网址:https://www.nuget.org/packages/BarCode/,让您可以开始使用IronBarcode自定义条形码。 如何在.NET C#中自定义条形码的背景颜色? 您可以使用IronBarcode的ChangeBackgroundColor方法在.NET C#中自定义条形码的背景颜色,提供独特的设计和品牌机会。 调整大小后,保持条形码可读性的最佳方法是什么? 为了在调整大小后保持可读性,使用IronBarcode的调整方法,确保条形码的尺寸不会影响其功能和清晰度。 Hairil Hasyimi Bin Omar 立即与工程团队聊天 软件工程师 如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。 准备开始了吗? Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,979,979 查看许可证