【问题标题】:Trouble with transfer function in WIA C#WIA C#中的传递函数问题
【发布时间】:2018-04-23 16:03:02
【问题描述】:

下面的代码有问题。我想通过单击 WinForms C# 应用程序中的按钮来扫描文档。

我使用 WIA、Visual Studio 和扫描仪 Fujitsu N7100A 与 Windows 8 配合使用。我正在关注使用 WIA 的在线教程。

但程序没有按预期运行。它似乎在 Transfer 方法上崩溃了。

            // Create a DeviceManager instance 
            var deviceManager = new DeviceManager();
            // Create an empty variable to store the scanner instance
            DeviceInfo firstScannerAvailable = null;
            // Loop through the list of devices to choose the first available 
            AddLogs(deviceManager.DeviceInfos.Count.ToString(), filename);
            foreach (DeviceInfo d in deviceManager.DeviceInfos)
            {
                if (d.Type == WiaDeviceType.ScannerDeviceType)
                {
                    firstScannerAvailable = d;
                }
            }
            // Connect to the first available scanner 
            var device = firstScannerAvailable.Connect();
            // Select the scanner 
            var scannerItem = device.Items[0];
            // Retrieve a image in JPEG format and store it into a variable 
            var imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatPNG);
            //Save the image in some path with filename 
            var path = @"C:\Documents\scan.png";
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            // Save image ! 
            imageFile.SaveFile(path);

我只需要删除日志文件中添加的行。

【问题讨论】:

  • 当我执行应用程序时,它给了我一个例外,说该值不包含在预期的 plage 中。

标签: c# windows wia


【解决方案1】:

这更像是一种解决方法,因为我不知道你的扫描仪。

我会假设所有扫描仪都有一个驱动器来存储他们扫描的文档,就像我的一样,所以我建议你阅读所有可用的驱动器循环通过它们检查 DriveType 和 VolumeLabel,然后读取它的文件并将文档复制到你想要的

类似这样的:

foreach (var item in DriveInfo.GetDrives())
        {
            //VolumeLabel differs from a scanner to another
            if (item.VolumeLabel == "Photo scan" && item.DriveType == DriveType.Removable)
            {
                foreach (var obj in Directory.GetFiles(item.Name))
                {
                    File.Copy(obj, "[YOUR NEW PATH]");
                    break;
                }
                break;
            }
        }

【讨论】:

  • 感谢您的回答。我的主要问题是:扫描仪此刻根本不扫描任何东西。它根本没有开始工作。我不知道为什么。如果我知道您的代码必须在扫描过程之后放置,对吗?
  • 如果它根本不起作用,我会认为它坏了,或者你的扫描仪可能需要它的驱动程序?
  • 扫描仪甚至可以与其他应用程序一起使用。
【解决方案2】:

最后一个 TWAIN 应用程序可以与此扫描仪配合使用。我会处理的。我没有说为什么要与 TWAIN 而不是 WIA 一起工作,而是说现实。很抱歉浪费了这么多时间。谢谢你的回答。祝你有美好的一天。

【讨论】:

    【解决方案3】:

    我目前正在解决这个问题。似乎N7100A驱动程序将devicePages属性设置为0,这应该意味着连续扫描,但是传输方法无法处理这个值。您必须将该属性设置为1

    var pages = 1;
    
    // Not all devices have this property, but Fujitsu N7100A has.
    device.Properties["Pages"]?.set_Value(ref pages);
    

    【讨论】:

      【解决方案4】:

      我认为问题出在这里

      var scannerItem = device.Items[0];
      

      因为 WIA 索引不是从零开始的,所以它应该是 1

      var scannerItem = device.Items[1];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        • 1970-01-01
        • 2020-04-15
        • 2022-11-29
        • 1970-01-01
        相关资源
        最近更新 更多