Capture Picture from WIA using C#

After much searching, I was almost frustrated as there was no simple solution to what I wanted to accomplish.
Make a simple C#/.Net/Desktop application that will:
1. Connect to a webcam
2. Show the video being "seen" by the webcam
3. Take a snapshot of the video and save it as an image.
The WIA (Windows Image Acquisition) worked wonders with the flatbed scanner. So I wanted to extend its use for video. 
Fortunately there is WIAVideo.dll that does exactly what I need to do.
Basically the steps are:
1. Detect the video input devices you have
2. get the device ID
3. Use WiaVideo to take a snapshot.
No screenshots this time.. but this is what I have
1. Create a form with two buttons and a picturebox
2. Download the WIA dlls and add references to them (WIA and WIAVideoLib)
3. Drop this code on your code-behind file
private void button2_Click(object sender, EventArgs e)
        {
            CommonDialogClass cdcMine = new CommonDialogClass();
            Device d = cdcMine.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
            if (d != null)
            {
                //get the devide ID
                this.DeviceID = d.DeviceID;
            }
            else
            {
                //no scanner chosen
                return;
            }
            CommonDialogClass WiaCommonDialog = new CommonDialogClass();
            DeviceManager manager = new DeviceManagerClass();
            Device WiaDev = null;
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                if (info.DeviceID == this.DeviceID)
                {
                    WIA.Properties infoprop = null;
                    infoprop = info.Properties;
                    //connect to scanner
                    WiaDev = info.Connect();
                    //not very good coding practice. exit the IF when connected
                    break;
                }
            }
            mVid.ImagesDirectory = imageDirectory;
            mVid.CreateVideoByWiaDevID(this.DeviceID, pictureBox1.Handle, 1, 1);  
        }
mVid is a WiaVideo object that you declare on form load as WiaVideo mVid = new WiaVideo();
The important part here is the imageDirectory variable, which is a string variable that was also instantiated and assigned during form load. If you do not provide a destination directory, it probably won’t run. :p
Then to take a picture
private void button1_Click(object sender, EventArgs e)
        {
            mVid.TakePicture(out imageDirectory);
        }
Couldn’t be any simpler than that. This part is where you will understand why mVid and imageDirectory were declared during load.
Also, don’t forget to call
mVid.DestroyVideo();
before closing the form!
that’s it! any questions, post them as comments here.
This entry was posted in Uncategorized. Bookmark the permalink.

15 Responses to Capture Picture from WIA using C#

  1. bob dreyer says:

    Does this work for C# 2010 express?
    Can I download the project/solution from somewhere?
    Thanks for recognizing the problem and giving a solution.

    • ezlorenzo says:

      hi Bob,

      I can’t see why it won’t work with express. With the proper DLLs in place, it should. Sad to say there is no sample project/solution for this but all the code I have here is all that is needed, really. I will deeply appreciate if you try it out and if there are missing parts, I’ll keep an eye out at this post so I can help. :)

  2. Pablo says:

    Hello!!!

    I’ve been looking like crazy for a sample code that simply takes a picture from any webcam plugged to my computer and store it where I want it. Yours seems to work fine and manage the situation quite simple, but I have an error in “this.DeviceID”. Do you have any idea why I have this error? I’m working on Visual Studio 2008, if that helps in anything.

    Thanks.

  3. @Pablo: You have to declare DeviceID!

    @ezlorenzo: looks great. Unfortunately it doesn’ work (Windows 7, Visual Studio 2008)
    Error occurs here:
    mVid.CreateVideoByWiaDevID(this.DeviceID, pictureBox1.Handle, 1, 1);

    Argument cannot be convertet from “System.IntPtr” to “ref WIAVIDEOLib._RemotableHandle”

    Would be so happy if you knew a solution for that.

    • ezlorenzo says:

      Oh my, this is over a year old.

      I will do my best to recreate your error johann. When I posted the original entry I was running on XP (I think) but am now on W7. Will update as soon as I can.

  4. Terry says:

    Waiting for your response whether it successfully runs on windows 7. I dont wish to use directshow. Pls help, your example is by far the easier to understand. :P

  5. Dan says:

    nicce job
    this snipets help me
    Thank u

  6. dvkch says:

    WIAVIDEOLib._RemotableHandle is a native int, if it can help.

  7. Kartheeswaran Jeyakumar says:

    Thanks for your sample code…It really Help me to in the right direction…But it could not Recognized the Webcam(PC 6009(CIF))
    .it shows the Following Exception: Exception from HRESULT: 0x80210015
    I used Windows Xp OS WIA 2.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s