WIA.DeviceManager.DeviceInfos.Count = 0。为什么?

发布于 2024-07-13 14:04:50 字数 1000 浏览 7 评论 0原文

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

到目前为止,我的操作如下:

  • 下载 wiaut.dll
  • 将其复制到 system32
  • 使用“regsvr32 wiaut.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 始终为空。 我连接了两台扫描仪,其中一台显示在“控制面板”->“扫描仪和相机”中,一台没有,并且两台扫描仪都显示在设备管理器的“成像设备”下。

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

在 Windows XP Service Pack 2 上运行

I'm trying to identify the scanners attached to the computer. One of the possible solutions is to use WIA (Windows Image Acquisition Automation Library).

These were my actions so far:

  • Download wiaaut.dll
  • Copy it to system32
  • Register it with "regsvr32 wiaaut.dll" (successfully)
  • Add reference to my project in Visual Studio.NET
  • Check that the Windows Image Acquisition (WIA) service is running

Next, I add and debug the following code:

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

However, the manager.DeviceInfos is always empty. I have 2 scanners attached, one of them shows in Control Panel->Scanners and Cameras, one doesn't, and both show under "Imaging Devices" in Device manager.

Any suggestion on why none are appearing in WIA.DeviceManager.DeviceInfos?

Running on Windows XP Service Pack 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行至春深 2024-07-20 14:04:50
 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 时将进行编译。
并且会跑..

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

try this:

deviceName = p.get_Value();

this will show like a error on visual studio but when you press f5 will compile.
and will run..

一抹苦笑 2024-07-20 14:04:50

尝试一下这个类:

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
}

}

try it with this class:

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
}

}

和我恋爱吧 2024-07-20 14:04:50

我在java中使用这个,所以也许我的提示不正确,但我只在Windows Vista及更高版本中使用你提到的方法......
所以看起来您正在使用 wia 2.0,但对于 Windows ME 和 XP,您应该使用 wia 1.0,网址

MSDN 描述了..
也许会有帮助

I work with this in java so maybe my hint isnt correct, but i use your mentioned way only for windows vista and later...
so it looks like you are using wia 2.0 but for windows ME and XP you should use wia 1.0

At MSDN it is described..
maybe it will help

猛虎独行 2024-07-20 14:04:50

尝试更改行 :

foreach (WIA.DeviceInfo info in manager.DeviceInfos)

并替换为:

foreach (manager.DeviceInfo info in manager.DeviceInfos)

我希望对您有所帮助。

Try change the line :

foreach (WIA.DeviceInfo info in manager.DeviceInfos)

and replace with:

foreach (manager.DeviceInfo info in manager.DeviceInfos)

I hope i helped you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文