Using C# and WIA 2.0 to connect to an imaging device

Welcome, in this article I am going to how to get your project setup to use WIA, how successfully connect to an image device and finally how to enumerate all the properties and events the device supports. While this article will only touch the surface of what all you can do with WIA subsequent articles will continue to delve deeper into WIA’s full capabilities.

Shall we get started?

Software & equipment needed

Getting Setup

Create a new Windows Forms Application and name it whatever you’d like, I named mine WIAIntroduction.

new-proj

Next add a label, command button and rich textbox control to Form1 (note:  I am leaving all controls at their default names for simplicity).

wiaaut-referenceNext we need to add a reference to the WIA library (wiaaut.dll). To this go to the Project->Add Reference menu. In the dialog box select the COM tab and navigate to Microsoft Windows Image Acquisition Library v2.0. If you do not see this make sure that you’ve installed the WIA Automation Component referenced above under Software & equipment needed.

I recommend ensuring that under the reference properties that Copy Local is set true. This will make a copy of the reference library in the root of the application (i.e. bin/release/).

With the reference to the WIA 2.0 library and our form and its controls setup we’re ready to begin communicating with any device with a WIA compliant driver.

Connecting to a Imaging Device with WIA 2.0

Now let’s get to the good stuff and make this application do something other than sit there.

First we need to add a using directive for the WIA library we just added a reference to so that we may begin to access the objects and methods it exposes.

using WIA;

Next create a private variable of the newly exposed type Device named _device.

Now we need to create a click event handler for button1. The code should look something like this.

private void button1_Click(object sender, EventArgs e){try{// create a new WIA common dialog box for the user to select a device fromCommonDialogClass dlg = new CommonDialogClass();// show user the WIA device dialogDevice d = dlg.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);// check if a device was selectedif (d != null){_device = d;}else{_device = null;}}catch(Exception ex){MessageBox.Show(ex.Message, "Error!",MessageBoxButtons.OK, MessageBoxIcon.Error);}}

Executing the code now and click on the button should produce a dialog box containing on all WIA devices registered on your PC.

Closing thoughts

In this article we learned how to quickly connect to any imaging device using Windows® Image Acquisition Automation Component.  In my next article I’ll discuss how to enumerate all the WIA properties and events that a device supports, until then happy coding.

fun=coding

 

Article source code:
EnumerateWIAProperties.rar

Related Posts:

  • No Related Posts

8 Responses to “Using C# and WIA 2.0 to connect to an imaging device”

  1. Rina says:

    I am new in WIA library.
    Is it WIA library support for NIKON D40X??

    Thank’s.

  2. flashy says:

    Any information on your sources though?

  3. I’ve been looking for this for a while now and found it here.

  4. Mike Bender says:

    Glad this was of help to you.

  5. Milan says:

    When is comming your next article about how to enumerate all the WIA properties and events that a device support? I want to take picture direct from c# app, is this posible. Canon a85.

  6. Mike Bender says:

    Milan, I apologize for my delinquency. I have the article pretty much written but just haven’t had the time to finish it up and get it published. I will try to make this a priority this coming week.

    On a side note I have actually began developing with Canon cameras as I’ve recently upgraded to a Canon EOS 50D (I love it!).

Leave a Reply