2016-07-27 216 views
1

我正在开发一个WPF应用程序来扫描不同的文档。文件的大小不一样,可以变化。WIA:设置动态页面大小

我有我的代码工作没有扫描仪对话框,我希望用户不必预览图像,然后扫描它得到真正的大小(导致两个扫描)。

的问题是,我尝试扫描

SetWIAProperty(item.Properties, "3097", 100); 

之前的页面大小设置为auto,但我得到HRESULT:0x80210067 System.Runtime.InteropServices.COMException。 我用google搜索了这个,发现我的扫描仪不支持这个属性。

那么,有什么办法可以达到这个目的吗?我需要得到的扫描图像只是文档,而不是所有的扫描仪区域(我现在正在进行这项工作)。 如果我不能告诉扫描仪只扫描文档,我也认为在裁剪生成的图像以获取我所需要的文档时,却不知道如何执行此操作。

这里是我的代码:

   DeviceManager deviceManager = new DeviceManager(); 
       Device scanner = null; 
       foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos) 
       { 
        if (deviceInfo.DeviceID == scannerId) 
        { 
         scanner = deviceInfo.Connect(); 
         break; 
        } 
       } 

       if (scanner == null) 
       { 
        throw new Exception("Scanner not found"); 
       } 

       Item item = scanner.Items[1] as Item; 
       int dpi = 300; 
       SetWIAProperty(item.Properties, "6146", 1); // 1 Color 
       SetWIAProperty(item.Properties, "6147", dpi); // dpis 
       SetWIAProperty(item.Properties, "6148", dpi); // dpis 
       // This line throws the exception 
       //SetWIAProperty(item.Properties, "3097", 100); // page size 0=A4, 1=letter, 2=custom, 100=auto 

       try 
       { 
        ICommonDialog wiaCommonDialog = new CommonDialog(); 
        ImageFile scannedImage = (ImageFile)wiaCommonDialog.ShowTransfer(item, FormatID.wiaFormatPNG, false); 

        if (scannedImage != null) 
        { 
         ImageProcess imgProcess = new ImageProcess(); 
         object convertFilter = "Convert"; 
         string convertFilterID = imgProcess.FilterInfos.get_Item(ref convertFilter).FilterID; 
         imgProcess.Filters.Add(convertFilterID, 0); 
         SetWIAProperty(imgProcess.Filters[imgProcess.Filters.Count].Properties, "FormatID", FormatID.wiaFormatPNG); 
         scannedImage = imgProcess.Apply(scannedImage); 
         if (System.IO.File.Exists(@"D:\temp\scanwia3.png")) 
          System.IO.File.Delete(@"D:\temp\scanwia3.png"); 
         scannedImage.SaveFile(@"D:\temp\scanwia3.png"); 
        } 
        scannedImage = null; 
       } 
       finally 
       { 
        item = null; 
        scanner = null; 
       } 

而且SetWIAProperty功能:

private static void SetWIAProperty(IProperties properties, object propName, object propValue) 
    { 
     Property prop = properties.get_Item(ref propName); 
     prop.set_Value(ref propValue); 
    } 

任何帮助,将不胜感激。

亲切的问候,

何塞。

回答

0

属性Page Size属于设备,而不是项目。

var WIA_IPS_PAGE_SIZE = "3097"; 
var WIA_PAGE_AUTO = 100; 

SetWIAProperty(scanner.Properties, WIA_IPS_PAGE_SIZE, WIA_PAGE_AUTO); 
+0

我试过这个,但我有同样的例外。我终于放弃了这一点,最后的方法是显示一个扫描预览对话框:用户首先预览并裁剪图像,然后扫描最终图像。谢谢你的时间。 –

+0

我使用NTwain进行扫描,并且默认情况下启用了自动调整大小。 – xiety

+0

也许NTwain提供了一些WIA无法直接实现的功能。但不幸的是,我现在不能改变为NTwain。 –