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.
This article is Part 2 in a 4-Part Series.
- Part 1 - How to Build .NET 6 Barcode and QR Code SDK for Windows, Linux & macOS
- Part 2 - Windows Barcode and QR Code Reader: Porting .NET Framework to .NET 6
- Part 3 - Mobile Barcode and QR Code Reader Using HTML5 and ASP.NET
- Part 4 - How to Build Windows Desktop Barcode and QR Code Scanner in .NET 6
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.
- Dynamsoft.DotNet.Barcode
-
The .NET Core package only supports .NET 5 and .NET Core 3.1. To be compatible with .NET 6, you can install BarcodeQRCodeSDK - a custom .NET 6 package built in previous article based on Dynamsoft C++ Barcode SDK.
License Activation
Click here to get a valid trial license.
Steps to Build Windows Form Barcode and QR Code Reader based on .NET Framework
- Create a new Windows Forms App project in Visual Studio.
- Install .NET Barcode and QR Code SDK via NuGet Package Manager.
-
Drag a
PictureBox
, aButton
, and aTextBox
from the toolbox to form designer. -
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=dcv&package=cross-platform BarcodeReader.InitLicense("<insert DBR license key here>", out errorMsg); BarcodeReader mBarcodeReader = new BarcodeReader();
-
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(); } }
-
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; }
-
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!"); }
-
Run the application.
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.
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:
- Copy the form designer code from the .NET framework project to the .NET 6 project.
-
Install
BarcodeQRCodeSDK
in terminal:dotnet add package BarcodeQRCodeSDK
-
Change
BarcodeReader
class toBarcodeQRCodeReader
class.BarcodeQRCodeReader.InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="); BarcodeQRCodeReader reader = BarcodeQRCodeReader.Create();
-
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 theDecodeBuffer()
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);
-
Run the .NET 6 application.
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-sdk/tree/main/example/desktop-gui