2

我正在尝试识别连接到计算机的扫描仪。一种可能的解决方案是使用 WIA(Windows 图像采集自动化库)。

到目前为止,这些是我的行动:

  • 下载 wiaaut.dll
  • 复制到system32
  • 使用“regsvr32 wiaaut.dll”注册它(成功)
  • 在 Visual Studio.NET 中添加对我的项目的引用
  • 检查 Windows 图像采集 (WIA) 服务是否正在运行

接下来,我添加并调试以下代码:

WIA.DeviceManager manager = new WIA.DeviceManagerClass();
string deviceName = "";
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
    if (info.Type == WIA.WiaDeviceType.ScannerDeviceType)
    {
        foreach (WIA.Property p in info.Properties)
        {
            if (p.Name == "Name")
                {
                    deviceName = ((WIA.IProperty)p).get_Value().ToString();
                    Console.WriteLine(deviceName);
                }
        }
    }
}

但是,manager.DeviceInfos 始终为空。我连接了 2 台扫描仪,其中一台显示在控制面板-> 扫描仪和相机中,一台没有,并且都显示在设备管理器的“成像设备”下。

关于为什么没有出现在 WIA.DeviceManager.DeviceInfos 中的任何建议?

在 Windows XP Service Pack 2 上运行

4

4 回答 4

1
 foreach (WIA.Property p in info.Properties)
     {
         if (p.Name == "Name") <-- p is a property why cast like you doing above.
         {
             deviceName = ((WIA.IProperty)p).get_Value().ToString();
             Console.WriteLine(deviceName);
         }
     }

试试这个:

deviceName = p.get_Value();

这将在 Visual Studio 上显示为错误,但当您按 f5 时将编译。并将运行..

于 2012-05-16T04:19:27.080 回答
0

试试这个类:

using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using WIAVIDEOLib;
namespace Scanner
{
public class ImageAcquisition
{

    private WIALib.WiaClass WiaClass;
    private WIALib.ItemClass ItemClass;
    private WIALib.CollectionClass CollectionClassDevices;
    private WIALib.CollectionClass CollectionClassPics;


    #region SelectDevice
    public bool SelectDevice()
    {
        try
        {
            object selectUsingUI;

            WiaClass = new WIALib.WiaClass();
            CollectionClassDevices = (WIALib.CollectionClass)WiaClass.Devices;

            if (WiaClass.Devices.Count == 0)
                return false;

            selectUsingUI = System.Reflection.Missing.Value;

            ItemClass = (WIALib.ItemClass)WiaClass.Create(ref selectUsingUI);

            if (ItemClass == null)
                return false;

            return true;
        }
        catch (System.Exception exp)
        {
            return false;
        }
    }
    #endregion

    #region Capture
    public System.Drawing.Image Capture()
    {
        try
        {
            CollectionClassPics = ItemClass.GetItemsFromUI(WIALib.WiaFlag.SingleImage, WIALib.WiaIntent.ImageTypeColor) as WIALib.CollectionClass;
            if (CollectionClassPics == null)
                return null;

            ItemClass = (WIALib.ItemClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(CollectionClassPics[0], typeof(WIALib.ItemClass));
            string imageFileName = System.IO.Path.GetTempFileName();
            ItemClass.Transfer(imageFileName, false);
            System.Drawing.Image Image = System.Drawing.Image.FromFile(imageFileName);

            System.Runtime.InteropServices.Marshal.ReleaseComObject(CollectionClassPics[0]);
            return Image;
        }
        catch (System.Exception exp)
        {
            return null;
        }
    }
    #endregion
}

}

于 2009-09-18T09:47:17.923 回答
0

我在java中使用这个,所以我的提示可能不正确,但我只将你提到的方式用于windows vista及更高版本......所以看起来你正在使用wia 2.0但对于windows ME和XP你应该使用wia 1.0

MSDN它被描述..
也许它会有所帮助

于 2010-11-23T14:42:29.327 回答
-2

尝试更改行:

foreach (WIA.DeviceInfo info in manager.DeviceInfos)

并替换为:

foreach (manager.DeviceInfo info in manager.DeviceInfos)

我希望我对你有所帮助。

于 2009-08-30T03:59:56.170 回答