'Scan with WIA. Set the properties and the drawer ADF

I need to use a scanner's ADF. I haven't found anything that works for me. I have tried this code (found on the net) but none of the settings are taking effect. The scanner window opens but the properties are not as set. Also I don't know the way, if the code worked, to set up the ADF. I tried this too: Scanning with C# and WIA

private void button2_Click(object sender, EventArgs e)
{
   WIA.CommonDialog _dialog = new CommonDialog();
   WIA.Device _scanner = _dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);

        foreach (Property item in _scanner.Items[1].Properties)
        {
            switch (item.PropertyID)
            {
                case 6146: //4 is Black-white,gray is 2, color 1
                    SetProperty(item, 2);
                    break;
                case 6147: //dots per inch/horizontal 
                    SetProperty(item, 100);
                    break;
                case 6148: //dots per inch/vertical 
                    SetProperty(item, 100);
                    break;
                case 6149: //x point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6150: //y-point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6151: //horizontal exent 
                    SetProperty(item, (int)(8.5 * 100));
                    break;
                case 6152: //vertical extent 
                    SetProperty(item, 11 * 100);
                    break;
            }
        }

        CommonDialog oDialog = new CommonDialog();
        oDialog.ShowAcquisitionWizard(_scanner);
   }



    private void SetProperty(Property property, int value)
    {
        IProperty x = (IProperty)property;
        Object val = value;
        x.set_Value(ref val);
    }


Solution 1:[1]

WIA in C# is murky and most of the learning happens via Trial-and-Error.

I'll give you some sample code for my usual Setup.

using WIA;

CommonDialog dialog = new CommonDialog();
DeviceManager manager = new DeviceManager();

// selecting device via UI
Device device = dialog.ShowSelectDevice();

// usually, the first item is the feeder
// depending on the device, the feeder is the only item
Item item = device.Items[1];

Now that you have a WIA.Item, you can Scan like so:

ImageFile image = (ImageFile)item.Transfer(FormatID.wiaFormatTIFF)

You can save this file by doing:

image.SaveFile("imageName.tiff");

To scan all Documents in your Feeder, you could use a while(true)-Loop, though you have to catch the Exception of an empty Feeder.

This, of course, will Scan with the preconfigured settings, so what if you wanted to modify your scan?

Let's say you wanted to change the "Document Handling Select"-Property of your Device. First, you have to find out the possible values for your Property. Every Property has a SubType, basically indicating a spectrum of possible values for your Property. E. g. WiaSubType.RangeSubType (an enum) indicates that you can enter a Range of Ints as a Value, e. g. from 0-100.

To find the SubType of a Property:

WriteLine(device.Properties["Document Handling Select"].SubType);
// FlagSubType

Print out possible values:

foreach(var value in device.Properties["Document Handling Select"].SubTypeValues)
{
    WriteLine(value);
}

With my Device (Fujitsu), I got these possible flags:

1
4
8
32
256
512

What do these mean? I have no clue.

To get the current Setting:

device.Properties["Document Handling Select"].get_Value();

In my case, 1.

To change the Setting:

device.Properties["Document Handling Select"].set_Value(4);

Of course, this is only possible because "Document Handling Select" isn't ReadOnly. To check if a Property is ReadOnly:

device.Properties["Document Handling Select"].IsReadOnly;

You can do this with any Property, either from an Item, a Device or else.

What I advise against are ugly, long switch-cases. Look into switch-expressions.

Here are more Samples, unfortunately in VB, though you could easily translate them and learn some VB on the go! Some might not work as this is for Wiaaut.

A nice Example for Scanning inside a WPF-App. Includes MVVM.

I hope this wasn't too late, or at least gets someone else started with WIA.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Halil Ibrahim Özcan