Coding Zen

my .NET coding endeavors and more

Need to convert a System.Drawing.Bitmap to BitmapImage? Try this helper out

Facebook
RSS

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:

  • Rina

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

    Thank’s.

    • Mike Bender

      Rina, yes the D40X is WIA compliant. Actually, the D40X is the camera I have been developing with as of late.

  • Rina

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

    Thank’s.

    • Mike Bender

      Rina, yes the D40X is WIA compliant. Actually, the D40X is the camera I have been developing with as of late.

  • http://mystuff.ask.com/public/klearchosm/folders/Guadalupe flashy

    Any information on your sources though?

    • Mike Bender

      The source code is available at the end of the article.

  • http://mystuff.ask.com/public/klearchosm/folders/Guadalupe flashy

    Any information on your sources though?

    • Mike Bender

      The source code is available at the end of the article.

  • http://www.squidoo.com/portlandphotoboothrental Photo booth rental

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

  • http://www.squidoo.com/portlandphotoboothrental Photo booth rental

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

  • Mike Bender

    Glad this was of help to you.

  • Mike Bender

    Glad this was of help to you.

  • Milan

    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.

  • Milan

    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.

  • Mike Bender

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

  • Mike Bender

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

  • Adel El-Rayyes

    I get the following error when i try your code:
    An unhandled exception of type ‘System.BadImageFormatException’ occurred in System.Windows.Forms.dll

    Additional information: Die Datei oder Assembly “Interop.WIA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null” oder eine Abhängigkeit davon wurde nicht gefunden. Es wurde versucht, eine Datei mit einem falschen Format zu laden.

    sorry it’s mostly in german but i hope you understand it anyway….

  • http://www.mobishoot.nl HdB

    Dear Mike, I can not download the EnumerateWIAProperties.rar?
    Please can you help me? Thanks i wnat to learn WIA with my new D5100 from Nikon.
    Regards Henk

  • Lance

    The link is broken! Yikes

  • Anirban

    I am using wia 2.0 in Windows7. I am trying to get image from my iball web cam using different code on internet. But the same error appears always. “exception from HResult: 0×80210015. I know this error arises when web cam is not detected but every time i connect the web cam and then test the code. Can anybody help. Thanks a lot in advance

Kinect for Windows i

[caption id="attachment_264" align="aligncenter" width="600" caption="Kinect for Windows Hardware buy yours ...

Kinect for Windows b

If you have been keeping up with the Kinect SDK ...

C# System.Drawing Im

I was recently presented with an issue of casting a ...

Kinect App Aids in T

I’ve been playing with the Microsoft Kinect and SDK at ...

Get those Camera Raw

If you are like me you have been long frustrated ...

Better Tag Cloud