Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WIA:设置dinamic页面大小_C#_.net_Wia_Page Size - Fatal编程技术网

C# WIA:设置dinamic页面大小

C# WIA:设置dinamic页面大小,c#,.net,wia,page-size,C#,.net,Wia,Page Size,我正在开发一个WPF应用程序,用扫描仪扫描不同的文档。文档的大小不会相同,可以是可变的 我的代码在没有扫描仪对话框的情况下工作,我希望用户不必预览图像,然后扫描它以获得真实大小(导致两次扫描) 问题是,我试图在扫描之前将页面大小设置为自动 SetWIAProperty(item.Properties, "3097", 100); 但是我得到了HRESULT:0x80210067 System.Runtime.InteropServices.COMException。 我用谷歌搜索了一下,发现我

我正在开发一个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);
    }
任何帮助都将不胜感激

亲切问候,


Jose.

属性
页面大小
属于设备,而不是项目

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

SetWIAProperty(scanner.Properties, WIA_IPS_PAGE_SIZE, WIA_PAGE_AUTO);

属性
页面大小
属于设备,而不是项目

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

SetWIAProperty(scanner.Properties, WIA_IPS_PAGE_SIZE, WIA_PAGE_AUTO);

我试过这个,但我也有同样的例外。我最终放弃了这一点,最后的方法是显示一个扫描预览对话框:用户首先预览并裁剪图像,然后扫描最终图像。感谢您的时间。我正在使用NTwain进行扫描,默认情况下它启用了自动大小。也许NTwain提供了一些我无法通过WIA直接实现的功能。但不幸的是,我现在不能换成NTwain。我试过这个,但我有同样的例外。我最终放弃了这一点,最后的方法是显示一个扫描预览对话框:用户首先预览并裁剪图像,然后扫描最终图像。感谢您的时间。我正在使用NTwain进行扫描,默认情况下它启用了自动大小。也许NTwain提供了一些我无法通过WIA直接实现的功能。但不幸的是,我现在不能换成新界北。