1

我正在开发一个 WPF 应用程序来使用扫描仪扫描不同的文档。文件的大小不会相同,可以是可变的。

我的代码在没有扫描仪对话框的情况下工作,我希望用户不必预览图像然后扫描它以获得真实尺寸(导致两次扫描)。

问题是我尝试在扫描之前将页面大小设置为自动

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

但我得到 HRESULT:0x80210067 System.Runtime.InteropServices.COMException。我用谷歌搜索了这个,发现我的扫描仪不支持这个属性。

那么,有没有办法实现这一目标?我需要生成的扫描图像只是文档,而不是所有扫描仪区域(我现在正在获取)。如果我不能告诉扫描仪只扫描文档,我也想过裁剪结果图像以仅获取我需要的文档,但现在不知道该怎么做。

这是我的代码:

                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);
    }

任何帮助,将不胜感激。

亲切的问候,

何塞。

4

1 回答 1

0

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

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

SetWIAProperty(scanner.Properties, WIA_IPS_PAGE_SIZE, WIA_PAGE_AUTO);
于 2017-01-24T11:34:15.073 回答