C# Barcode Reader > C# Barcode Scanner Tutorial > How to scan, read QR Code in C# Windows Forms application using C# Barcode Reader library?

How to scan, read QR Code in C# WinForms application?

Detailed tutorial for reading and scanning barcode image in C# Windows Forms (WinForms) app

C# Barcode Reader Library

Scan QR Code, Data Matrix, Code 128, GS1, EAN/UPC from image files in WPF app.

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




How to scan, read QR Code images in C# WinForms?

Here we will provide a step-by-step C# Windows Forms project tutorial to enable QR Code scanning function in a new WinForms project using BarcodeLib C# Barcode Reader library. By the end of guide, you will get a simple WinForms application with QR Code scanner feature included.





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.QRCodeReader.WinForms.Demo. 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 QR Code scanner library

Add the following QR Code 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.

  1. One button with name btnFile. Text is Select File. Click on the button, the app will popup a dialog to select a QR Code image file to scan.
  2. One text box with name tbFilePath. The text box will display the selected QR Code image file path.
  3. One button with name btnScan. Text is Scan QR Code. Click the button to start scanning QR Code image.
  4. One picture box with name pbImage. It will display the scanning image on the form. Set property SizeMode to Zoom
  5. One text box with name tbResult. It will display the QR Code image scanning result.
Design and align the controls on the form.



4. Add C# code to scan QR Code from the image file

Here will add C# code to scan QR Codes 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.Drawing;
    using System.Text;
    
    namespace BarcodeLib.QRCodeReader.WinForms.Demo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.tbFilePath.ReadOnly = true;
                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(BarcodeReader.BarcodeReader.QRCODE);
    
                    BarCode[] barcodes = BarcodeReader.BarcodeReader.ReadBarcode(filePath, options);
    
                    //  Show scan result in the scan result TextBox.
                    StringBuilder sb = new StringBuilder();
                    if (barcodes.Length > 0)
                    {
                        sb.Append("Number of QR Code(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 QR Code 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 QR Code Reader Windows Forms application from Visual Studio. Select a QR Code image, scan and view the results.

Related Barcode Library Tutorials