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 https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&source=codepool
     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;
                
             try
             {
                 bitmap = new Bitmap(dlg.FileName);
             }
             catch (Exception exception)
             {
                 MessageBox.Show(exception.ToString());
                 return;
             }
    
             pictureBox1.Image = bitmap;
             textBox1.Clear();
         }
     } 
        
    
  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(result);
             textBox1.AppendText(Environment.NewLine);
         }
     }
     else
     {
         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.InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="); 
     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;
             break;
         case PixelFormat.Format32bppArgb:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_ARGB_8888;
             break;
         case PixelFormat.Format16bppRgb565:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_565;
             break;
         case PixelFormat.Format16bppRgb555:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_RGB_555;
             break;
         case PixelFormat.Format8bppIndexed:
             format = BarcodeQRCodeReader.ImagePixelFormat.IPF_GRAYSCALED;
             break;
     }
    
     string[]? results = reader.DecodeBuffer(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, format);
     bitmap.UnlockBits(bmpData);
    
  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

https://github.com/yushulx/dotnet-barcode-qr-code-reader