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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Evgeny
  • 3,320
  • 7
  • 39
  • 50

4 Answers4

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

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..

0

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
  • 1,374
  • 1
  • 10
  • 21
0

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

Zavael
  • 2,383
  • 1
  • 32
  • 44
  • 1
    You don't have to use wia 1.0 for xp. Check http://msdn.microsoft.com/en-us/library/ms630827(v=vs.85).aspx It says: Applications that use the WIA Automation Layer API require Windows XP Service Pack 1 (SP1) or later. Earlier versions of Windows are not supported. – elif Jan 25 '11 at 14:45
-2

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.