Windows Barcode and QR Code Reader: Porting .NET Framework to .NET 6

Microsoft is pushing .NET technologies to the next level. It is no doubt that .NET framework will be retired in the near future. This article aims to help C# developers to create Windows desktop barcode and QR code reader application using Dynamsoft Barcode Reader SDK. We will firstly create a Windows form application based on the .NET framework template, and then port the .NET framework project to .NET 6 project step by step.

Install .NET Barcode and QR Code SDK

Dynamsoft has published two .NET packages for scanning barcode and QR code. One for .NET Framework and the other for .NET Core.

License Activation

Click here to get a valid trial license.

Steps to Build Windows Form Barcode and QR Code Reader based on .NET Framework

  1. Create a new Windows Forms App project in Visual Studio.
  2. Install .NET Barcode and QR Code SDK via NuGet Package Manager.
  3. Drag a PictureBox, a Button, and a TextBox from the toolbox to form designer.

    windows form designer

  4. Set the license key and initialize the barcode and QR code reader.

     // Get a license key from
     BarcodeReader.InitLicense("<insert DBR license key here>", out errorMsg);
     BarcodeReader mBarcodeReader = new BarcodeReader();
  5. In button click function, open a file dialog to load image files:

     using (OpenFileDialog dlg = new OpenFileDialog())
         dlg.Title = "Open Image";
         dlg.Filter = "Image files (*.bmp, *.jpg, *.png) | *.bmp; *.jpg; *.png";
         if (dlg.ShowDialog() == DialogResult.OK)
             Bitmap bitmap = null;
                 bitmap = new Bitmap(dlg.FileName);
             catch (Exception exception)
             pictureBox1.Image = bitmap;
  6. Decode barcode and QR code from an image bitmap:

     TextResult[] textResults = mBarcodeReader.DecodeBitmap(bitmap, "");
     if (textResults != null)
         string[] results = new string[textResults.Length];
         int index = 0;
         foreach (TextResult result in textResults)
             results[index++] = result.BarcodeText;
         return results;
  7. Display the decoded results:

     if (results != null)
         foreach (string result in results)
         textBox1.AppendText("No barcode detected!");
  8. Run the application.

    • .NET Framework barcode reader

Porting .NET Framework Project to .NET 6 Project

If you have never touched .NET 6 before, you can create a .NET 6 project in Visual Studio and make a quick comparison with the .NET framework project.

.NET 6 vs .NET Framework

We can see both projects have similar structure. The code of form designer is the same. The only difference is the dependency packages.

Here are the porting steps:

  1. Copy the form designer code from the .NET framework project to the .NET 6 project.
  2. Install BarcodeQRCodeSDK in terminal:

     dotnet add package BarcodeQRCodeSDK
  3. Change BarcodeReader class to BarcodeQRCodeReader class.

     BarcodeQRCodeReader reader = BarcodeQRCodeReader.Create();
  4. Since .NET 6 does not support Bitmap class, we need to get byte array, image width, image height, image format and image stride from the image bitmap as the input parameters of the DecodeBuffer() method:

     BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
     ImageLockMode.ReadWrite, bitmap.PixelFormat);
     BarcodeQRCodeReader.ImagePixelFormat format = BarcodeQRCodeReader.ImagePixelFormat.IPF_ARGB_8888;
     switch (bitmap.PixelFormat)
         case PixelFormat.Format24bppRgb:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_888;
         case PixelFormat.Format32bppArgb:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_ARGB_8888;
         case PixelFormat.Format16bppRgb565:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_565;
         case PixelFormat.Format16bppRgb555:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_555;
         case PixelFormat.Format8bppIndexed:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_GRAYSCALED;
     string[]? results = reader.DecodeBuffer(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, format);
  5. Run the .NET 6 application.

    .NET 6 desktop barcode and QR code reader

    The .NET 6 Windows Form App performs no different than the .NET Framework Windows Form App.

Source Code