Using Dynamsoft Barcode Reader .NET API in WinForms Application

Dynamsoft's Barcode Reader SDK provides .NET API which enables you to almost instantly embed barcode reading functionality in your .NET desktop or web applications using C# or VB.NET.

In this video, I am going to demonstrate how to use the .NET barcode reading API to quickly build a WinFroms Application for 1D and 2D barcode recognition.

The detailed steps

  • New a barcode project magnify

    1.New a barcode project

    First, let's create a new project. Click Templates -> Visual C# -> Windows -> Windows Forms Application. Change the name to BarcodeTest.

    Drag a button to the form. And then double click to add code for its click event.

    New a barcode project close
    New a barcode project
  • Add references magnify

    2.Add references

    First, we need to add reference. Right click References to add Dynamsoft.BarcodeReader.dll.

    The DLL can be found in the installation directory \Program Files (x86)\Dynamsoft\Barcode Reader 5.2\Components\DotNet.

    Add references close
    Add references
  • Add namespace magnify

    3.Add namespace

    Then, we add the namespace.

    using Dynamsoft.Barcode;

    Add namespace close
    Add namespace
  • Initiate Barcode magnify

    4.Initiate Barcode

    BarcodeReader reader = new BarcodeReader();

    Initiate Barcode close
    Initiate Barcode
  • Barcode-reader-options magnify

    5.Barcode-reader-options

    With the following snippet, we initialize the barcode reading options,
    such as barcode types, how many barcodes to read per page etc.

    ReaderOptions option = new ReaderOptions();
    option.BarcodeFormats = BarcodeFormat.OneD;
    option.MaxBarcodesToReadPerPage = 100;
    reader.ReaderOptions = option;
                    
    Barcode-reader-options close
    Barcode-reader-options
  • Decode the barcode magnify

    6.Decode the barcodes

    Call the DecodeFile method to decode the barcodes. If there are multiple barcodes found,
    we use a loop to print out the results one by one.

    try
    {
      BarcodeResult[] results = reader.DecodeFile(@"D:\Program Files (x86)
      \Dynamsoft\Barcode Reader 5.2\Images\AllSupportedBarcodeTypes.tif");
      string strInfo = "Total barcode(s) found: " + 
      results.Length.ToString() + ".\n";
      for (int i = 0; i < results.Length; ++i)
      {
        BarcodeResult barcode = results[i];
        strInfo += "Barcode " + (i+1).ToString() + ":\n";
        strInfo += barcode.BarcodeFormat.ToString() + "\n";
        strInfo += barcode.BarcodeText + "\n\n";
      }
      MessageBox.Show(strInfo);
    }
      catch (BarcodeReaderException exp)
    {
      MessageBox.Show("Error Code: " + exp.Code.ToString() 
      + "\nError String: " + exp.Message);
    }
                  
    Decode the barcode close
    Decode the barcode
  • Barcode Result magnify

    7.Barcode Result

    Build and debug the project.

    Click the button. OK. We've got all the barcodes recognized.

    Barcode Result close
    Barcode result