How to Read Barcode in WPF with VB.NET

We’re proud to reveal that Dynamsoft’s Dynamic .NET TWAIN 5.0 is on its way to release soon. In addition to WinForms support, the new version will include a WPF control. You can use it in your WPF app to capture images from TWAIN scanners, to edit and save images to local disks, to a server or a database. Today, I’d like to cover how, using VB.NET, you can use the Dynamic .NET TWAIN 5.0 SDK to implement barcode reading in a WPF application.

The relevant source code can be downloaded here:
Download sample code

1. Create a New WPF Project

First, start Visual Studio .NET, and create a new WPF project. Select Visual Basic as the type and choose WPF Application as the template.

2. Add a Reference

Right-click on the project root, and select Add Reference… from the menu. Click Browse to locate DynamicDotNetTWAIN.dll and add it.

3. Add Dynamic .NET TWAIN Component

If there is no Dynamic .NET TWAIN component available, you might need to open the toolbox. To do so, right-click on the panel and select Choose Items Switch to the tab WPF Components and click Browse to load DynamicDotNetTWAIN.Wpf.dll

4. Add Buttons and Textbox

Next, you’ll want to drag two buttons and one textbox from the toolbox to design a form. The buttons are used to load an image and make the detection of a barcode. And, the textbox is used to display the information decoded from barcode.

5. Code for Loading Image

Now, what you need to do is to acquire an image using the Windows standard API. Then you’ll need to load it using the Dynamic .NET TWAIN component method. Just a couple of lines of code are needed for loading an image. It’s pretty simple.

Dim filedlg As OpenFileDialog
 filedlg = New OpenFileDialog()
 filedlg.Multiselect = True
Dim strFilename As String
 If (filedlg.ShowDialog() = DialogResult.OK) Then
     For Each strFilename In filedlg.FileNames
         DynamicDotNetTwain1.LoadImage(strFilename)
     Next
 End If

6. Code for Barcode Recognition

To decode a barcode, the path for the barcode library needs to be first be specified. Then, with one additional line of code, you’ll get results.

Set the path of barcode library

Dim strDllFolder As String
strDllFolder = Application.ExecutablePath
Dim pos As Integer
pos = strDllFolder.LastIndexOf("\Samples\")
If (pos <> -1) Then
    strDllFolder = strDllFolder.Substring(0, strDllFolder.IndexOf("\", pos)) + "\Bin\BarcodeResources\"
End If
Me.DynamicDotNetTwain1.BarcodeDllPath = strDllFolder

Get the results and display them on screen

Me.TextBox1.Text = ""
Dim aryResult() As Result
aryResult = Me.DynamicDotNetTwain1.ReadBarcode(Me.DynamicDotNetTwain1.CurrentImageIndexInBuffer, BarcodeFormat.All)
Dim strText As StringBuilder
strText = New StringBuilder()
If aryResult.Length = 1 Then
    strText.AppendFormat(aryResult.Length & " total barcode" & ("") & " found." & Constants.vbCrLf)
Else
    strText.AppendFormat(aryResult.Length & " total barcode" & ("s") & " found." & Constants.vbCrLf)
End If
For i As Integer = 0 To aryResult.Length - 1
    Dim objResult As Result
    objResult = aryResult(i)
    strText.AppendFormat("      Result " & (i + 1) & Constants.vbCrLf)
    strText.AppendFormat("      BarcodeFormat: " & objResult.BarcodeFormat.ToString() & Constants.vbCrLf)
    strText.AppendFormat("      Text read: " & objResult.Text & Constants.vbCrLf)
Next i
Me.TextBox1.Text = strText.ToString()

I hope this brief tutorial is helpful to anyone who is looking for a .NET barcode reader SDK in VB.NET.

Dynamic .NET TWAIN 5.0 is scheduled to be released late next month. To stay informed about all our product announcements, blog posts and more, be sure to follow us on Twitter, like us on Facebook or add us on Google+. Also, if you’d like to be amongst the first to try out the new version first, please contact support[at]dynamsoft.com.

P.S. if you need to read barcode from a web application, please check How to Read Barcodes Online from a Web Application for more details.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com