An intro to WIA (Windows Image Acquisition) in C# (Part 1)

Windows Image Acquisition is a platform for accessing images from cameras, scanners, and webcams. Recently I’ve been working on a project that involves a scanner. As it turns out for basic use WIA is very easy to use, however once you attempt to do more advanced things the friendly resources begin to dwindle leaving you with the cold emotionlessness of MSDN. However, this being my first major project I refused to give up and finally found out how to do almost everything I wanted to do. In this post, the first of this series, I’ll talk about acquiring an image from a flatbed scanner.

First let’s start by looking at the easiest way to acquire an image from a scanner. Begin by adding a reference to the Microsoft Windows Image Acquisition Library v2.0, this can be found under COM.

Don’t forget to put the proper using statement at the top of your code.

using WIA;

Before we create our scanning function we need to create two objects, one for access to WIA’s common dialogs and another to hold our image after it is scanned.

CommonDialog dialog = new WIA.CommonDialog();
ImageFile scannedImage = null;

Now let’s create a function to scan our document and save it.

void Scan()
{
    scannedImage = dialog.ShowAcquireImage(
                        WiaDeviceType.ScannerDeviceType,
                        WiaImageIntent.UnspecifiedIntent,
                        WiaImageBias.MaximizeQuality,
                        FormatID.wiaFormatPNG,
                        true, true, false);
    scannedImage.SaveFile("scannedImage.png");
}

As if by magic when Scan() is called a wizard appears and walks you through scanning a document. If you are acquiring only one image and don’t want any further customization to the interface then you can stop here and you won’t be missing anything.

However I found this wizard to be too slow. I wanted my users to be able to click one button and have the scanner jump into action.

To start we need to add two more WIA objects to our code. A DeviceManger object and a device object.

DeviceManager deviceManager = new DeviceManagerClass();
Device scanner = null;

WIA has another really nice dialog in its CommonDiolog class that allows the user to select a Device and the dialog will actually return the device for you.

scanner = dialog.ShowSelectDevice(
          WiaDeviceType.ScannerDeviceType, false, false);

Now that you have the device selected lets modify our Scan() method to scan with our newly selected device.

Void Scan()
{
    Item item = device.Items[1];
    scannedImage = dialog.ShowTransfer(item,
                   FormatID.wiaFormatPNG, false) as ImageFile;
    scannedImage.SaveFile(“scannedImage.png”)
}

Now we are back where we were before but this time running the Scan() method will tell the scanner to immediately start scanning and our user won’t have to sit though a multipage wizard.

I think this is a good point to stop for now. In a little while Ill post the second part to this post covering how to change properties such as DPI and switching between grayscale and color.

This entry was posted in Programing, Tutorials and tagged , . Bookmark the permalink.

2 Responses to An intro to WIA (Windows Image Acquisition) in C# (Part 1)

  1. JEOVANY TENORIO HERNANDEZ says:

    Me parece muy bueno de tu parte compartir tus conocimientos, mi problema que tengo es que no he encontrado algun tutorial de como adquirir imagenes desde un escaner plano con C# 2005 no se si me podrias ayudar por favor gracias de antemano

  2. Yasir says:

    Hi, the post is absolutely nice. one thing i want to ask is that if i get the device from the method
    dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false,false);
    after that i want to show the dialog regarding the image size which can be opened using the method
    dialog.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.UnspecifiedIntent, WiaImageBias.MaximizeQuality, FormatID.wiaFormatBMP, true, true, false);
    so by using the above method the scanner selection dialog also opens before the actual dialog at where we can specify the image size. so is there any way to skip the scanner selection dialog at that point as i have already get the device.
    please reply me as soon as possible. waiting for your positive response.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>