使用 IRONBARCODE 如何在 C# Windows 應用程式中使用條碼掃描器 Curtis Chau 更新:2025年11月10日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 本教學將以 IronBarcode 函式庫為例,示範如何在 C# 控制台應用程式和 .NET Windows 窗體應用程式中掃描二維碼和條碼。 使用 IronBarcode 庫,可以同時掃描和讀取多個條碼,並且可以成功掃描不完美的影像。 我們先來明確一下什麼是條碼掃描器。 什麼是條碼掃描器? 條碼是由一系列寬度不一的平行黑線和白色空白組成的方形或長方形影像。 條碼掃描器或條碼閱讀器是一種可以讀取列印的條碼、解碼條碼中包含的資料並將資料傳送到電腦的裝置。 以下步驟將介紹如何使用 IronBarcode 庫建立條碼掃描器。 如何在 C# 中讀取條碼 在 Microsoft Visual Studio 中建立一個 .NET Windows 窗體應用程式專案 安裝條碼庫 讀取任何條碼或二維碼 一次掃描即可讀取多個條碼或二維碼 允許 IronBarcode 讀取不完美的掃描件和照片 1. 在 Microsoft Visual Studio 中建立一個 Windows 窗體應用程式 開啟 Visual Studio > 按一下"建立新專案" > 選擇"Windows 表單應用程式範本" > 按下"下一步" > 為專案命名 > 按"下一步" > 選擇目標 .NET Framework > 按一下"建立"按鈕。 專案建立完成後,使用 Visual Studio 工具箱中的下列控制項設計窗體:PictureBox、Label、TextBox 和 Button 控制項。 如何在 C# Windows 應用程式中使用條碼掃描器,圖 1:條碼掃描器 條碼掃描器 2. 在 C# 中安裝 Barcode .NET 函式庫 條碼庫可以透過以下三種方法之一進行安裝: 1. 軟體包管理器控制台 在軟體包管理器控制台中輸入以下命令。 它將為您下載並安裝該軟體包。 Install-Package BarCode 2. NuGet 套件管理器解決方案 您也可以使用 NuGet 套件解決方案安裝條碼庫。 只需按照以下步驟操作: 按一下"工具" > "NuGet 套件管理員" > "管理解決方案的 NuGet 套件" 。 這將開啟 NuGet 套件管理器。 點擊"瀏覽"並蒐索"條碼",然後安裝類別庫。 3. 從連結下載 作為替代方案,您可以下載IronBarCode.Dll並將其作為引用新增至您的專案。 下載完成後,將以下引用新增至您的條碼閱讀器專案。 using IronBarCode; using IronBarCode; Imports IronBarCode $vbLabelText $csharpLabel 3. 讀取任何條碼或二維碼 使用 IronBarcode 庫和.NET 條碼閱讀器在 .NET 中讀取條碼或二維碼非常容易。 條碼掃描器 在您的專案中,瀏覽找到您想要讀取的圖像。 它將在PictureBox中開啟; 現在點選"掃描二維碼"。 文字將顯示在文字方塊中。 以下是用於開啟圖片的"瀏覽"按鈕的程式碼: // Open file dialog OpenFileDialog open = new OpenFileDialog(); // Image filters open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp"; if (open.ShowDialog() == DialogResult.OK) { // Display image in PictureBox pictureBox1.Image = new Bitmap(open.FileName); // Store image file path in class data member. Initialize it as string ImageFileName; ImageFileName = open.FileName; } // Open file dialog OpenFileDialog open = new OpenFileDialog(); // Image filters open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp"; if (open.ShowDialog() == DialogResult.OK) { // Display image in PictureBox pictureBox1.Image = new Bitmap(open.FileName); // Store image file path in class data member. Initialize it as string ImageFileName; ImageFileName = open.FileName; } ' Open file dialog Dim open As New OpenFileDialog() ' Image filters open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp" If open.ShowDialog() = DialogResult.OK Then ' Display image in PictureBox pictureBox1.Image = New Bitmap(open.FileName) ' Store image file path in class data member. Initialize it as string ImageFileName; ImageFileName = open.FileName End If $vbLabelText $csharpLabel "掃描二維碼"按鈕的代碼: // Read the barcode from the image file path BarcodeResult Result = BarcodeReader.Read(ImageFileName); // Display the decoded text in TextBox textBox1.Text = Result.Text; // Read the barcode from the image file path BarcodeResult Result = BarcodeReader.Read(ImageFileName); // Display the decoded text in TextBox textBox1.Text = Result.Text; ' Read the barcode from the image file path Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName) ' Display the decoded text in TextBox textBox1.Text = Result.Text $vbLabelText $csharpLabel 條碼掃描器會在文字方塊中顯示條碼數據,如下所示: 如何在 C# Windows 應用程式中使用條碼掃描器,圖 2:要使用 C# 掃描的條碼影像 使用 C# 掃描條碼影像 QR 圖碼掃描器 本節中,IronBarcode 庫有效地處理了涉及傾斜二維碼的現實世界情況。 雖然傾斜角度的二維碼可以透過Read方法處理和讀取,但解決起來仍然需要更多的時間。 IronBarcode 庫提供了一種自訂方式,可以使用BarcodeReaderOptions作為額外的參數來處理此類影像輸入。 代碼如下: // Define a collection of image filters to apply var filtersToApply = new ImageFilterCollection() { new SharpenFilter(), new InvertFilter(), new ContrastFilter(), new BrightnessFilter(), new AdaptiveThresholdFilter(), new BinaryThresholdFilter() }; // Configure barcode reader options with specified filters BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() { ImageFilters = filtersToApply, ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, }; // Read the barcode/QR code with custom options and display result BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample); textBox1.Text = Result.Text; // Define a collection of image filters to apply var filtersToApply = new ImageFilterCollection() { new SharpenFilter(), new InvertFilter(), new ContrastFilter(), new BrightnessFilter(), new AdaptiveThresholdFilter(), new BinaryThresholdFilter() }; // Configure barcode reader options with specified filters BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() { ImageFilters = filtersToApply, ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, }; // Read the barcode/QR code with custom options and display result BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample); textBox1.Text = Result.Text; ' Define a collection of image filters to apply Dim filtersToApply = New ImageFilterCollection() From { New SharpenFilter(), New InvertFilter(), New ContrastFilter(), New BrightnessFilter(), New AdaptiveThresholdFilter(), New BinaryThresholdFilter() } ' Configure barcode reader options with specified filters Dim myOptionsExample As New BarcodeReaderOptions() With { .ImageFilters = filtersToApply, .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128 } ' Read the barcode/QR code with custom options and display result Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName, myOptionsExample) textBox1.Text = Result.Text $vbLabelText $csharpLabel 打開傾斜的二維碼影像後,輸出結果如下: 如何在 C# Windows 應用程式中使用條碼掃描器,圖 4:傾斜的二維碼影像 傾斜的二維碼影像 單次掃描讀取多個條碼 PDF文檔 可以從 PDF 文件中掃描條碼圖像,並根據需要適當地顯示每個結果。 以下範例程式碼可讓您從 PDF 檔案中讀取多個條碼。 // Scan for multiple barcodes within a PDF document BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf"); // Work with the results found foreach (var PageResult in PDFResults) { string Value = PageResult.Value; int PageNum = PageResult.PageNumber; System.Drawing.Bitmap Img = PageResult.BarcodeImage; BarcodeEncoding BarcodeType = PageResult.BarcodeType; byte[] Binary = PageResult.BinaryValue; Console.WriteLine(PageResult.Value + " on page " + PageNum); } // Scan for multiple barcodes within a PDF document BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf"); // Work with the results found foreach (var PageResult in PDFResults) { string Value = PageResult.Value; int PageNum = PageResult.PageNumber; System.Drawing.Bitmap Img = PageResult.BarcodeImage; BarcodeEncoding BarcodeType = PageResult.BarcodeType; byte[] Binary = PageResult.BinaryValue; Console.WriteLine(PageResult.Value + " on page " + PageNum); } ' Scan for multiple barcodes within a PDF document Dim PDFResults() As BarcodeResult = BarcodeReader.ReadPdf("MultipleBarcodes.pdf") ' Work with the results found For Each PageResult In PDFResults Dim Value As String = PageResult.Value Dim PageNum As Integer = PageResult.PageNumber Dim Img As System.Drawing.Bitmap = PageResult.BarcodeImage Dim BarcodeType As BarcodeEncoding = PageResult.BarcodeType Dim Binary() As Byte = PageResult.BinaryValue Console.WriteLine(PageResult.Value & " on page " & PageNum) Next PageResult $vbLabelText $csharpLabel PDF 檔案中的條碼和二維碼: 如何在 C# Windows 應用程式中使用條碼掃描器,圖 3:C# - 從 PDF 結果中讀取條碼 C# - 從 PDF 結果中讀取條碼 從不完美影像中讀取條碼 在實際應用中,條碼在影像、掃描件、縮圖或照片中經常會出現瑕疵,並且可能包含數位雜訊或出現傾斜。 本節示範如何從縮圖中讀取條碼資料。 縮圖 IronBarcode 庫使用C# 條碼產生器,它甚至能夠讀取損壞的條碼縮圖。 自動校正條碼縮圖尺寸。 使用 IronBarcode 在 C# 中讀取文件 它會自動偵測太小而無法合理表示實際條碼的影像,然後放大並清除與縮圖相關的所有數位噪聲,從而使它們再次可讀。 // Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise. BarcodeResult SmallResult = BarcodeReader.Read("ThumbnailOfBarcode.gif"); // Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise. BarcodeResult SmallResult = BarcodeReader.Read("ThumbnailOfBarcode.gif"); ' Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise. Dim SmallResult As BarcodeResult = BarcodeReader.Read("ThumbnailOfBarcode.gif") $vbLabelText $csharpLabel 摘要 IronBarcode 是一個功能強大的 .NET 軟體庫和C# QR 碼產生器,用於掃描和讀取各種條碼影像格式,無論這些條碼是完美的螢幕截圖,還是實際上是照片、掃描件或其他不完美的真實世界影像,它都能勝任。 此外,IronBarcode 還提供各種自訂選項來提高條碼讀取速度,例如裁剪區域或多線程,以及提高 ML 模型的準確性。 請造訪官方文件頁面,以了解有關 IronBarcode 的更多資訊。 目前,如果您購買完整的 Iron Suite ,您只需支付兩個庫的價格即可獲得五個庫。 常見問題解答 在 C# 應用程式中,條碼掃描器是什麼? 條碼掃描器是一種讀取印刷條碼、解碼訊息並將其發送到電腦的裝置。在 C# 應用程式中,可以使用 IronBarcode 等程式庫來實現此功能。 如何使用 C# 建立一個用於條碼掃描的 Windows 窗體應用程式? 要使用 C# 建立用於條碼掃描的 Windows 窗體應用程序,請開啟 Visual Studio,使用「Windows 窗體應用程式範本」建立新項目,配置目標 .NET Framework,並使用 PictureBox、Label、TextBox 和 Button 等控制項來設計窗體。 在 C# 專案中安裝條碼庫的建議方法是什麼? 您可以透過套件管理器控制台使用Install-Package IronBarCode在 C# 專案中安裝 IronBarcode 等條碼庫,也可以透過 NuGet 套件管理器安裝,或下載 DLL 並將其新增為參考。 是否可以使用 C# 函式庫一次掃描讀取多個條碼? 是的,使用 IronBarcode,您可以使用BarcodeReader.ReadPdf方法在一次掃描中讀取多個條碼,即使是 PDF 文件中的條碼也可以讀取。 圖書館是如何從低品質影像中讀取條碼的? IronBarcode 可透過應用影像濾波器和放大技術來減少數位噪聲,從而解讀低品質影像中的條碼,確保準確讀取。 IronBarcode 等 C# 函式庫支援哪些條碼格式? IronBarcode 支援多種條碼格式,包括二維碼和 Code128。即使影像不完美或使用相機拍攝,它也能讀取這些格式。 在.NET應用程式中實作條碼讀取的步驟是什麼? 若要實現條碼讀取,請將圖像載入到 PictureBox 中,觸發「掃描代碼」操作,然後使用 IronBarcode 處理解碼後的文字並將其顯示在 TextBox 中。 IronBarcode能否有效處理傾斜或歪斜的二維碼? 是的,IronBarcode 可以透過使用BarcodeReaderOptions應用必要的影像濾鏡和調整,有效處理傾斜的二維碼,從而實現準確讀取。 IronBarcode 提供哪些條碼讀取的自訂功能? IronBarcode 提供裁剪區域、多執行緒和參數調整等功能,以提高條碼讀取速度和準確性。 哪裡可以找到關於在 C# 中使用條碼庫的更詳細資訊? 有關在 C# 中使用條碼庫的更多詳細信息,您可以訪問 Iron Software 網站上的官方文件頁面。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2026年1月22日 ASP.NET 條碼掃描器教學:C# 條碼產生器指南 學習如何使用 IronBarcode 在 ASP.NET 中掃描條碼 閱讀更多 發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多 發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多 條碼產生器 .NET 教學課程如何使用 C# 在 ASP.NET 中產...
發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多
發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多