USING IRONBARCODE How to Print Barcode in C# Windows Application Jordi Bardia 已更新:六月 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 1.0 介绍 将数据以可见、机器可读的格式呈现的一种方式是通过使用条形码。 起初,平行线被间隔、加宽和调整大小以在条形码中表示数据。 这些现代线性或一维(1D)条形码可以由专门的光学扫描仪读取,这些扫描仪被称为条形码阅读器,其中有多种类型。 后来,创建了二维(2D)变体,称为矩阵码或2D条形码,尽管它们并不真正使用条形。 这些变体使用矩形、点、六边形和其他图案代替传统条形码。 专门设计用于读取2D条形码的2D光学扫描仪有多种配置可用。 读取2D条形码的另一种方法是使用与运行软件的计算机相连的数码相机,该软件拍摄条形码的照片并使用图像对其进行解码。 后一种形式的2D条形码扫描仪可以由带集成相机的移动设备(如智能手机)使用,方法是安装专门的应用程序软件。 2.0 IronBarcode 特性 使用 IronBarcode 的条形码库可以轻松生成动态条形码。 这个简单的库只需几行代码就能生成条形码。 IronBarcode 的条形码阅读器包括强大的条形码生成器,使它们能够生成高质量的条形码。 这使得条形码扫描仪读取您的条形码变得简单。 IronBarcode 可以读取和写入大多数条形码格式和QR标准,包括 UPC A/E、Databar、EAN 8/13、MSI、Code 39/93/128、CodaB、RSS 14/Expanded 和 ITF。 在读取扫描和实时视频帧时,IronBarcode 能够校正旋转、噪音、失真和倾斜。 在生成条形码时,IronBarcode 会自动预处理条形码图片以提高读取速度和精度。 动态条形码很受欢迎,因为它们允许内容修改。 IronBarcode 可以利用多个核心和线程,对于批量处理服务器非常有利。 在单页和多页文档中,IronBarcode 可以自动找到一个或多个条形码。 IronBarcode 支持 32 位和 64 位架构,并兼容 .NET Framework 和 .NET Core 实现。 IronBarcode 支持 PC 和移动平台上的控制台、桌面、云和在线应用程序。 IronBarcode 可以为各种文件和流类型创建条形码图像,包括 PDF、JPG、TIFF、GIF、BMP、PNG 和 HTML。 3.0 在 Visual Studio 中创建新项目 要使用 IronBarcode 框架,首先必须创建 Visual Studio .NET 项目。 任何版本的 Visual Studio 都可以使用,但建议使用最新版本。 根据您的需求,您可以创建 .NET Windows 窗体应用程序或从各种项目模板中选择。 对于本教程,我们将使用 Windows 窗体应用程序以保持简单。 输入项目的名称和位置。 本项目将使用 .NET Framework 4.7。 创建项目后,Form1.cs 文件将以设计器视图打开。 您可以插入程序代码、设计用户界面并构建/运行程序。 要在解决方案中使用 IronBarcode 库,您需要下载所需的软件包。 这可以通过在包管理器控制台中使用以下代码来完成: Install-Package BarCode 或者,您可以使用 NuGet 包管理器搜索并下载 "Barcode" 软件包,该软件包将列出所有搜索结果。 从那里,您可以选择所需的软件包下载。 在我们的表单中,我们放置了一个 SaveFileDialog 框,允许我们将生成的条形码图像保存到选定的位置。 4.0 使用 IronBarcode 生成条形码 IronBarcode 库允许我们仅用几行代码快速生成条形码。 以下是在 Windows 表单中生成条形码标签的示例代码: using IronBarCode; // Import IronBarcode namespace for barcode generation using System; using System.Windows.Forms; // For creating Windows Forms applications namespace IronBarcode_demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Initializes the form components } private void button1_Click(object sender, EventArgs e) { try { saveFileDialog1.Filter = ".png|*.png"; // Set file filter for saving as PNG DialogResult result = saveFileDialog1.ShowDialog(); // Show save file dialog if (result == DialogResult.OK) { string filename = saveFileDialog1.FileName; // Get the filename chosen by the user // Create a QR code using data from textBox1, and save it as a PNG QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng(filename); MessageBox.Show("Barcode Generated Successfully"); // Inform user of success } } catch (Exception ex) { MessageBox.Show(ex.Message); // Display error message in case of exception } } } } using IronBarCode; // Import IronBarcode namespace for barcode generation using System; using System.Windows.Forms; // For creating Windows Forms applications namespace IronBarcode_demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Initializes the form components } private void button1_Click(object sender, EventArgs e) { try { saveFileDialog1.Filter = ".png|*.png"; // Set file filter for saving as PNG DialogResult result = saveFileDialog1.ShowDialog(); // Show save file dialog if (result == DialogResult.OK) { string filename = saveFileDialog1.FileName; // Get the filename chosen by the user // Create a QR code using data from textBox1, and save it as a PNG QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng(filename); MessageBox.Show("Barcode Generated Successfully"); // Inform user of success } } catch (Exception ex) { MessageBox.Show(ex.Message); // Display error message in case of exception } } } } Imports IronBarCode ' Import IronBarcode namespace for barcode generation Imports System Imports System.Windows.Forms ' For creating Windows Forms applications Namespace IronBarcode_demo Partial Public Class Form1 Inherits Form Public Sub New() InitializeComponent() ' Initializes the form components End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Try saveFileDialog1.Filter = ".png|*.png" ' Set file filter for saving as PNG Dim result As DialogResult = saveFileDialog1.ShowDialog() ' Show save file dialog If result = System.Windows.Forms.DialogResult.OK Then Dim filename As String = saveFileDialog1.FileName ' Get the filename chosen by the user ' Create a QR code using data from textBox1, and save it as a PNG QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng(filename) MessageBox.Show("Barcode Generated Successfully") ' Inform user of success End If Catch ex As Exception MessageBox.Show(ex.Message) ' Display error message in case of exception End Try End Sub End Class End Namespace $vbLabelText $csharpLabel 在开始代码之前,请添加一个文本框到 .NET WinForms 应用程序。 这将允许我们输入文本以生成条形码。 然后在 Windows 窗体应用程序中添加一个按钮,并从示例代码中添加所需的代码。 我们还使用了 SaveFileDialog 工具,它将有助于将生成的条形码图片保存到所需的位置。 当用户点击 "保存条形码" 按钮时,将弹出 "另存为" 对话框,允许用户选择文件名和保存生成的条形码图片为 PNG 文件的位置。条形码是基于文本框中输入的文本生成的。 CreateQrCode 函数的唯一必需参数是需要在 QR 码图像中编码的数据(我们从文本框中获取的字符串或流)。 该方法还接受三个附加的可选参数: 图形的默认大小是 500 像素宽乘 500 像素高。 错误修正的级别。 IronBarcode 有四个级别的错误修正:低、中、高和最高。在创建 QR 码时,默认使用最高级别的修正 (QRCodeWriter.QrErrorCorrectionLevel.greatest)。 QR 码的版本号。 如果值为 0(默认值),则方法会根据将要编码的数据使用适当的版本号。 上面的示例使用中等程度的错误修正创建了一个 500 x 500 像素的图形。 通过对生成的自定义 QR 码使用 SaveAsPng 函数,我们可以在从 SaveAs 文件对话框中获得的指定文件位置将 QR 码保存为 PNG 文件。 点击 此处 获取更全面的 IronBarcode 指南。 5.0 结论 IronBarcode 库被认为是生成和识别条形码的最佳选择之一,因为它的效率和与各种操作系统的兼容性。 它提供了一系列用于创建和定制不同条形码类型的功能,包括调整文本、颜色、线宽和高度的能力。 库的许可详细信息可以在 网站 上获得,包括开发者的付费和免费版本。 更新和支持免费提供一年。 常见问题解答 如何在C# Windows窗体应用程序中生成条形码? 要在C# Windows窗体应用程序中生成条形码,您可以通过NuGet包管理器集成IronBarcode库。创建一个文本框以输入条形码数据,并使用IronBarcode提供的示例代码创建一个按钮来触发条形码生成。 在设置Visual Studio项目以生成条形码时涉及哪些步骤? 首先,通过Visual Studio使用.NET Framework 4.7或更高版本设置Windows窗体应用程序。然后,使用NuGet包管理器安装IronBarcode包以启用条形码生成功能。 如何将生成的条形码保存为图像文件? IronBarcode允许您将生成的条形码保存为PNG文件。您可以在Windows窗体应用程序中使用 'SaveFileDialog' 工具选择保存条形码图像的文件名和位置。 使用二维条形码而不是传统线性条形码的优点是什么? 二维条形码(如二维码)可以存储比传统线性条形码更多的数据,并且可以由数码相机或专业光学扫描仪读取,使其在各种应用中更具多功能性。 生成二维码时可以自定义错误纠正级别吗? 是的,IronBarcode允许您在生成二维码时设置错误纠正级别。根据您对二维码抵抗损坏或数据丢失的要求,此设置可以调整为低、中、高或最高。 使用IronBarcode处理批量条形码是否可能? 是的,IronBarcode支持通过利用多个核心和线程进行批量处理,这对于需要大容量条形码处理的服务器端应用程序尤为有用。 IronBarcode可以在哪些平台上使用? IronBarcode兼容.NET Framework和.NET Core,支持32位和64位架构。它可以用于控制台应用程序、桌面应用程序、云服务以及PC和移动平台上的在线应用程序。 为什么图像预处理在条形码生成中很重要? 图像预处理非常重要,因为它通过矫正旋转、噪声、失真和倾斜等问题来提高条形码读取的精度和速度,从而确保高质量的条形码生成和识别。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已发布十月 19, 2025 How to Print Barcodes in Crystal Reports with VB.NET Generate and print barcodes in Crystal Reports using VB.NET. Step-by-step tutorial with IronBarcode SDK for reliable barcode integration. 阅读更多 已发布九月 29, 2025 IronBarcode vs. Open-Source Barcode Readers in .NET Learn how to read barcodes in C# using IronBarcode 阅读更多 已发布九月 29, 2025 How to Scan Barcodes in an ASP.NET Application Learn how to Scan Barcodes in ASP.NET using IronBarcode 阅读更多 How to Generate QR Code in .NET MAUIHow to Dynamically Generate and Dis...
已发布十月 19, 2025 How to Print Barcodes in Crystal Reports with VB.NET Generate and print barcodes in Crystal Reports using VB.NET. Step-by-step tutorial with IronBarcode SDK for reliable barcode integration. 阅读更多
已发布九月 29, 2025 IronBarcode vs. Open-Source Barcode Readers in .NET Learn how to read barcodes in C# using IronBarcode 阅读更多
已发布九月 29, 2025 How to Scan Barcodes in an ASP.NET Application Learn how to Scan Barcodes in ASP.NET using IronBarcode 阅读更多