天天看点

如何通过C#使用Dynamsoft Barcode Reader控件创建一个简易的桌面程序

本篇文章介绍如何在桌面应用程序中用C#创建一个简易的“Hello World”程序。

步骤:

1. 启动Visual Studio并使用C#创建新的Windows窗体应用程序。

2. 右键单击解决方案资源管理器中的引用,将Dynamsoft.BarcodeReader.dll添加到项目中。

    注意:DLL可以在安装文件夹中找到。通常,他们在以下目录:

    如果你使用 .NET Framework 2.0-3.5:

    [安装文件夹] \ Components \ DotNet \ 2.0 \

    如果你使用 .NET Framework 4.0及更高版本:

    [安装文件夹] \ Components \ DotNet \ 4.0 \

3. 添加命名空间

using Dynamsoft.Barcode;
           

4. 在表单上创建一个新按钮,并使用以下代码替换button1_Click的内容。

注意:请在代码中将

"<your image file full path>"

 and 

"<your license key here>"替换成正确的图片路径和许可证

如果您已经安装了Dynamsoft Barcode Reader SDK 30天免费试用版,则可以在目录C:\ Program Files(x86)\ Dynamsoft \ Barcode Reader {版本号} \ LicenseManager.exe中找到该许可证。

private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         BarcodeReader dbr = new BarcodeReader();
         dbr.LicenseKeys = "<your license key here>";
         TextResult[] aryResult = dbr.DecodeFile("<your image file full path>", ""); // leave the template name empty ("") will use  default settings for recognition

         if (aryResult.Length == 0)
         {
             Console.WriteLine("No barcode found.");
         }
         else
         {
             for (var i = 0; i < aryResult.Length; i++)
             {
                 Console.WriteLine("Barcode: {0}", (i + 1));
                 Console.WriteLine("BarcodeFormat: {0}", aryResult[i].BarcodeFormat.ToString());
                 Console.WriteLine("BarcodeText: {0}", aryResult[i].BarcodeText);
             }
         }
     }
     catch (BarcodeReaderException exp)
     {
         Console.WriteLine("Error: {0}, {1}", exp.Code.ToString(), exp.Message);
     }
 }
           

5. 运行该项目。

部署应用程序时,请确保DLL与EXE文件位于同一文件夹中。

如果您对Dynamsoft Barcode Reader SDK感兴趣,可以点击此链接下载30天试用安装包。

继续阅读