Dev Center
Table of contents

Getting Started with .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, 2019, 2022
  • Developing Environment:
    • Visual Studio 2008 or above
    • .NET Framework 2.0, .NET Framework 4.0 or above
    • .NET Core 3.1
    • .NET 5.0 or 6.0

Note: Dynamsoft Barcode Reader provides both online and offline license options. The online license option might not work in an environment that doesn’t have network connection. In such case, you can get an offline trial license key via Customer Portal or by contacting us.

Installation

If you haven’t downloaded the SDK yet, download the .NET Package now from Dynamsoft website and unzip the package into the directory of your choice. After you unzip the file, you can find samples for supported platforms in the Samples folder under the installation folder.

For this tutorial, the folder we unzip the SDK to will be referred to as [INSTALLATION FOLDER], so please be aware of that as you go through the rest of this guide.

You can also download the Dynamsoft.DotNet.Barcode nuget Package which supports .NET Framework 2.0/4.0, .NET Core 3.1, .NET 5.0/6.0.

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 here.

Create a New Project

  1. Start Visual Studio and create a new Console Application in C#. Let’s name it DBRCSharpSample.

Add the Library Reference

  1. 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 Select Dynamsoft.BarcodeReader.dll and DynamsoftCommon.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.
  2. Add the namespace in Program.cs.

     using Dynamsoft;
     using Dynamsoft.DBR;
    

Initialize a Barcode Reader Instance

  1. Initialize the license key.

     string errorMsg;
     EnumErrorCode errorCode = BarcodeReader.InitLicense("<insert DBR license key here>", out errorMsg);
     if (errorCode != EnumErrorCode.DBR_SUCCESS)
     {
         // Add your code for license error processing;
         Console.WriteLine(errorMsg);
     }
    

    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.
  2. Create an instance of Dynamsoft Barcode Reader

     BarcodeReader reader = new BarcodeReader();
    

    However, please note that if you are using a concurrent instance license, please use the new APIs GetInstance to initialize the barcode reader instance and then Recycle to allow for better concurrent instance management by the library.

     BarcodeReader reader = BarcodeReader.GetInstance();
     // If no instance is available right away, the application will wait until one becomes available
     if (reader != null)
     {
         // Add your code here to call decoding method, process barcode results and so on
         // ...
         // Recycle the instance to make it idle for other concurrent tasks
         reader.Recycle();
     }
    

Configure the Barcode Scanning Behavior

  1. Set barcode format and count to read.

     PublicRuntimeSettings settings = reader.GetRuntimeSettings();
     settings.BarcodeFormatIds = (int)EnumBarcodeFormat.BF_PDF417;
     settings.BarcodeFormatIds_2 = (int)EnumBarcodeFormat_2.BF2_DOTCODE;
     settings.ExpectedBarcodesCount = 2;
     reader.UpdateRuntimeSettings(settings);
    

    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.

    The Barcode Reader SDK comes with a large array of runtime settings to optimize the performance of the library. To learn about all the runtime settings, please visit the RuntimeSettings API page. To learn more about the cases and situations in which the settings can help, please visit the Explore Features page.

Decode and Output Results

  1. Decode barcodes from an image file.
  2. Get and output barcode results.

     try
     {
         TextResult[] results = reader.DecodeFile(@"[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", "");
         if (results != null && results.Length > 0)
         {
             Console.WriteLine("Total barcodes found: " + results.Length);
             for (int i = 0; i < results.Length; ++i)
             {
                 Console.WriteLine("Barcode " + (i + 1));
                 Console.WriteLine("    Barcode Format: " + results[i].BarcodeFormatString);
                 Console.WriteLine("    Barcode Text: " + results[i].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

  1. Destroy the instance to release all resources.

     reader.Dispose();
    

    However, please note that if you are using a concurrent instance license, please use the new APIs Recycle to allow for better concurrent instance management by the library.

     if (reader != null)
     {
         reader.Recycle();
     }
    

Note:
Please change all [INSTALLATION FOLDER] in above code snippet to your unpacking path.

Build and Run the Project

  1. In Visual Studio, build the project to generate program DBRCSharpSample.exe.

  2. Run the program DBRCSharpSample.exe.

If you got the exception “Failed to create compression directory” or “Failed to load module dll”, please copy x64 and x86 folders to the folder where Dynamsoft.BarcodeReader.dll and DynamsoftCommon.dll resides and try again. The x64 and x86 folders can be found under [INSTALLATION FOLDER]\Lib\2.0 or [INSTALLATION FOLDER]\Lib\4.0.

You can download the entire source code here.

Next Steps

This page is compatible for:

Is this page helpful?

YesYes NoNo

In this article:

version 9.6.20

  • Latest version
  • Version 10.x
    • Version 10.4.10
    • Version 10.2.10
    • Version 10.2.0
    • Version 10.0.20
    • Version 10.0.10
    • Version 10.0.0
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.30
    • Version 9.6.20
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.0
    • Version 9.0.0
  • Version 8.x
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
  • Documentation Homepage
Change +