User Guide for .Net
In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using .Net.
Requirements
- Operating Systems:
- Windows 7, 8, 10, 11
- Windows Server 2003, 2008, 2008 R2, 2012, 2016
- Developing Environment:
- Visual Studio 2008 or above
- .NET Framework 2.0, .NET Framework 4.0 or above
- .NET Core 3.1 or 5.0
Installation
If you haven’t downloaded the SDK yet, download the .NET Package
now from Dynamsoft website and unpack the package into the directory of your choice.
For this tutorial, we unpack it to
[INSTALLATION FOLDER]
, change it to your unpacking path for the following content.
After you unzip the file, you can find samples for supported platforms in the Samples folder under the installation folder.
The .NET SDK is developed based on .NET Framework 2.0/4.0. If you are using .NET Core 3.1 or 5.0, please download the .NET Core SDK (Dynamsoft.NetCoreApp.Barcode) from Here.
Build Your First Application
Let’s start by creating a console application which demonstrates how to use the minimum code to read barcodes from an image file.
You can download the entire source code and compiled program from Here.
Create a New Project
- Start Visual Studio and create a new Console Application in C#. Let’s name it
DBRCSharpSample
.
Add the Library Reference
- Follow one of the below steps to add Dynamsoft Barcode Reader SDK to your project.
- Right click on Project -> Add -> Reference, Browse to
[INSTALLATION FOLDER]\Lib\4.0
and SelectDynamsoft.BarcodeReader.dll
andDynamsoftCommon.dll
.Browse to
[INSTALLATION FOLDER]\Lib\2.0
if you want to use .NET Framework 2.0. - Right click on Project -> Manage NuGet Packages, Search and Install package
Dynamsoft.DotNet.Barcode
.
- Right click on Project -> Add -> Reference, Browse to
- Add the namespace in
Program.cs
.using Dynamsoft; using Dynamsoft.DBR;
Initialize a Barcode Reader Instance
-
Create an instance of Dynamsoft Barcode Reader.
BarcodeReader reader = new BarcodeReader("<insert DBR license key here>")
Please replace
<insert DBR license key here>
with a valid DBR licensekey. There are two ways to obtain one:- Search
DBRLicense
and find the license from[INSTALLATION FOLDER]\Samples\BarcodeReaderDemo\BarcodeReaderDemo\App.config
. - Request a trial license from Customer Portal.
- Search
Configure the Barcode Scanning Behavior
-
Set barcode format and count to read.
PublicRuntimeSettings runtimeSettings = reader.GetRuntimeSettings(); runtimeSettings.BarcodeFormatIds = (int)EnumBarcodeFormat.BF_ALL; runtimeSettings.BarcodeFormatIds_2 = (int)(EnumBarcodeFormat_2.BF2_POSTALCODE | EnumBarcodeFormat_2.BF2_DOTCODE); runtimeSettings.ExpectedBarcodesCount = 32; reader.UpdateRuntimeSettings(runtimeSettings);
The barcode formats to enable is highly application-specific. We recommend that you only enable the barcode formats your application requires. Check out Barcode Format Enumeration for full supported barcode formats.
If you know exactly the barcode count you want to read, specify
ExpectedBarcodesCount
to speed up the process and improve the accuracy.
Decode and Output Results
- Decode barcodes from an image file.
-
Get and output barcode results.
TextResult[] results; try { results = reader.DecodeFile(@"[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", ""); Console.WriteLine("Total barcodes found: " + results.Length); for (int iIndex = 0; iIndex < results.Length; ++iIndex) { Console.WriteLine("Barcode " + (iIndex + 1)); if (results[iIndex].BarcodeFormat != 0) { Console.WriteLine(" Barcode Format: " + results[iIndex].BarcodeFormatString); } else { Console.WriteLine(" Barcode Format: " + results[iIndex].BarcodeFormatString_2); } Console.WriteLine(" Barcode Text: " + results[iIndex].BarcodeText); } } catch (BarcodeReaderException exp) { Console.WriteLine(exp.Message); }
For the error handling mechanism, the SDK throws BarcodeReaderException for each function. You should add codes for exception handling based on your needs.
The SDK returns multiple barcode information, including barcode count, barcode format, barcode text, location, barcode raw data, etc. Check out TextResult for full supported result data.
Release Resource
-
Destroy the instance to release all resources.
reader.Dispose();
Note:
Please change all[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
Build and Run the Project
-
In Visual Studio, build the project to generate program
DBRCSharpSample.exe
. -
Run the program
DBRCSharpSample.exe
.
If you got the exception “Failed to create compression directory” or “Failed to load module dll”, please copy
x64
andx86
folders to the folder whereDynamsoft.BarcodeReader.dll
andDynamsoftCommon.dll
resides and try again. Thex64
andx86
folders can be found under[INSTALLATION FOLDER]\Lib\2.0
or[INSTALLATION FOLDER]\Lib\4.0
.
You can download the entire source code and compiled program from Here.