天天看點

如何通過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天試用安裝包。

繼續閱讀