Home > C# Barcode Reader > How to read barcode in C#? > C#.NET Barcode Reader SDK for .NET Windows Application, Free to Download Trial Package

C# Barcode Reader library for .NET WinForms, WPF Windows Applications

Barcode Reader for .NET is the best .NET / C# barcode image recognition component in the market.
Barcode Reader C# library supports scan and recognize linear & 2d barcode images in all .NET Desktop projects:
  • Windows Forms App (.NET Core and .NET Framework)
  • WPF Application, Class Library, Custom Control Library, User Control Library (.NET Core and .NET Framework)
  • Windows Service (.NET Framework)
  • .NET Console Applications
The following tutorial will show you how to scan, read QR Code and barcodes from existing image files using C# in your .NET Windows Forms desktop applications.

C# Barcode Reader Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Multi-thread reading in C# Windows application.

Download Now
NuGet Ready
.NET 5+
.NET Framework 4.x, 3.x, 2.x


How to scan, read barcodes in C# WinForms?

1. Create new Windows Forms project

  1. Start Visual Studio 2022 and choose Create a new project.

  2. Choose Windows Forms App in the dialog, and click Next button.

  3. Create a new project with name BarcodeLib.BarcodeReader.WinFormsDemo. Click Next button.

  4. Choose Framework as .NET 8.0 (Long Term Support), and Click Create.

  5. Now we have created a new C# Windows Forms project successfully.


2. Add barcode scanner library

Add the following Barcode Reader library dll and nuget package to the Windows Forms project.
  • Add barcode reader library .net standard dll BarcodeLib.BarcodeReader.Common.dll to the project reference
  • Search and install nuget System.Drawing.Common (latest stable version) from NuGet Package Manager in Visual Studio


3. Add, design controls on Form

Add the following Windows Forms controls to Form1.

  • One button with name btnFile. Text is Select File. Click on the button, the app will popup a dialog to select an image file to scan barcode.
  • One text box with name tbFilePath. The text box will display the selected image file path.
  • One label with default name. Text is Barcode Type : .
  • One combo box with name cbBarcodeType. The list shows all supported barcode types by the barcode reader.
  • One button with name btnScan. Text is Scan Image. Click the button to start scanning barcode image.
  • One picture box with name pbImage. It will display the scanning image on the form.
  • One text box with name tbResult. It will display the barcode image scanning result.
Design and align the controls on the form.



4. Add C# code to read barcode

Here will add C# code to scan barcodes from selected image file.
  1. Righ click file Forms1.cs in Solution Explorer, and choose View Codefrom the menu to open the Form1.cs window.
  2. Copy and replace the Form1.cs content with the following C# codes.
    The C# codes below will set the form UI control initial states, and C# codes to handle the two buttons' click events.

    using BarcodeLib.BarcodeReader;
    using System;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BarcodeLib.BarcodeReader.WinFormsDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.tbFilePath.ReadOnly = true;
                this.cbBarcodeType.Items.AddRange(new String[] {
                    "Codabar", "Code 128 / GS1-128", "Code 39", "Code 39 Full ASCII",
                    "Interleaved 2 of 5", "EAN-13", "EAN-8",
                    "UPC-A", "UPC-E", "QR Code", "PDF-417",
                     "Data Matrix", "IDENTCODE", "Code 93", "Patch Code",
                    "POSTNET","PLANET","RM4SCC", "Australia Post",
                    "Intelligent Mail (OneCode)"
                });
                this.cbBarcodeType.SelectedIndex = 1;
                this.btnScan.Enabled = false;
                this.tbResult.ReadOnly = true;
                this.tbResult.Multiline = true;
            }
    
            private void btnFile_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.tbFilePath.Text = openFileDialog.FileName;
                        this.btnScan.Enabled = true;
                        this.pbImage.Image = Image.FromFile(openFileDialog.FileName);
                    }
                    catch (Exception) { }
                }
            }
    
            private void btnScan_Click(object sender, EventArgs e)
            {
                try
                {
                    String filePath = this.tbFilePath.Text;
                    if (String.IsNullOrEmpty(filePath))
                        throw new Exception("File path is null or empty.");
                    if (!File.Exists(filePath))
                        throw new Exception("File does not exist.");
    
    
                    OptimizeSetting options = new OptimizeSetting(this.cbBarcodeType.SelectedIndex);
    
                    BarCode[] barcodes = BarcodeReader.ReadBarcode(filePath, options);
    
                    //  Show scan result in the scan result TextBox.
                    StringBuilder sb = new StringBuilder();
                    if (barcodes.Length > 0)
                    {
                        sb.Append("Number of Barcode(s) : " + barcodes.Length + "\r\n\r\n");
                        int index = 1;
                        foreach (BarCode barcodeData in barcodes)
                        {
                            sb.Append(index + ". [" + barcodeData.GetTypeName() + "]: " 
                                + barcodeData.GetMessage() + "\r\n");
                        }
                            
                    }
                    else
                    {
                        sb.Append("No Barcode Detected.");
                    }
    
                    this.tbResult.Text = sb.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
    
  3. Go to Form1.cs [Design] in Visual Studio.
    Select button btnFile, in Properties window, add Click event handler method btnFile_Click
    Select button btnScan, in Properties window, add Click event handler method btnScan_Click


5. Run Windows Forms application

Now we run the Barcode Reader Windows Forms application from Visual Studio. Select a barcode image, scan and view the results.





Conclusion

Now you have learned how to quickly read barcodes from image files in C# Windows applications. You can further customize and optimze the BarcodeLib .NET Barcode Reader C# library to parse advanved barcode data and improve the recognition speed.

How to read a barcode data message from multiple barcodes in C#?
How to read valid GS1 data message from GS1 barcodes (QR Code, Data Matrix, GS1-128) using C#?
How to read Unicode characters from barcodes in C#?
How to scan multiple barcode image files using C#?





















Also provides .NET Barcode Reader, ASP.NET Barcode Reader, C# Barcode Reader, VB.NET Barcode Reader, .NET QR Code Reader, ASP.NET QR Code Reader, C# QR Code Reader, VB.NET QR Code Reader, .NET Data Matrix Reader, .NET Code 128 Reader, Java Barcode SDK, .NET Barcode SDK, ASP.NET Barcode SDK, Visual C# Barcoding, Visual Basic .NET Barcoding.