views:

1166

answers:

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

A: 

Hi!

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.

Unfortunately this way it would not even compile ...
Evgeny
A: 

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
}

}

Meysam Javadi