2010-12-05 78 views
0

我是silverlight的新手,我正在嘗試使用wia掃描儀集成。我知道usia WIA.CommonDialog,showacquireimage()我可以從掃描儀中檢索圖像。我試圖直接訪問設備並執行掃描命令以避免用戶交互。WIA silverlight掃描儀集成

我可以連接到設備。但掃描儀可用的唯一命令是同步的。我試圖在設備對象上使用ExecuteCommand,但我不確定要使用什麼命令。任何方向將不勝感激。

 using (dynamic DeviceManager1 = AutomationFactory.CreateObject("WIA.DeviceManager")) 
     { 
      var deviceInfos = DeviceManager1.DeviceInfos; 
      for(int i= 1;i<=deviceInfos.Count;i++) 
      { 
       //check if the device is a scanner 
       if (deviceInfos.Item(i).Type.ToString() == "1") 
       { 
        var IDevice = deviceInfos.Item(i).Connect(); 
        deviceN.Text = IDevice.Properties("Name").Value.ToString(); 

        var dv = IDevice.Commands; 
        for (int j = 0; j <= dv.Count; j++) 
        { 

         deviceN.Text += " " + dv.Item(i).CommandID.ToString() + " " + dv.Item(i).Description.ToString(); 
        } 

       } 

      }    
     } 

回答

1

您並不需要處理設備命令來掃描文檔。相反,您需要使用設備對象下的第一個設備項目。這裏有一個小例子,它本身起作用,沒有失敗檢查以便於閱讀。

//using WIA; 

bool showProgressBar = false; 

DeviceManager deviceManager = new DeviceManagerClass(); 

foreach(DeviceInfo info in deviceManager.DeviceInfos) 
{ 
    if(info.Type == WiaDeviceType.ScannerDeviceType) 
    { 
     Device device = info.Connect(); 

     ImageFile imageFile = null; 

     Item deviceItem = null; 

     //Read through the list of items under the device... 
     foreach(Item item in device.Items) 
     { 
      //Pick the very first one! 
      deviceItem = item; 
      break; 
     } 

     if(showProgressBar == true) 
     { 
      //Scan without GUI, but display the progress bar dialog. 
      CommonDialogClass commonDialog = new CommonDialogClass(); 
      imageFile = (ImageFile)commonDialog.ShowTransfer(deviceItem, FormatID.wiaFormatBMP, false); 
     } 
     else 
     { 
      //Scan without GUI, no progress bar displayed... 
      imageFile = (ImageFile)deviceItem.Transfer(FormatID.wiaFormatBMP); 
     } 

     imageFile.SaveFile("C:\\image.bmp"); 
    } 
} 

掃描之前(但你連接到設備項目後),你可能需要設置不同的設備屬性中選擇掃描分辨率,色深和其他的東西,如果默認值不適合您的需求不夠好。

不久前,我做了一個易於使用的類來操作WIA兼容的掃描儀,你可以從this page下載...它是爲.Net Framework 2.0,C#。也許它可以方便地在你的項目中使用,你可以用幾行代碼完成,包括設置基本屬性。 :)