Short solution for short problems

WIA vs DirectShow.Net

In the previous post I described how to take a picture using WIa. I looked a little further and discovered the DirectShow library. They have some good samples and just the code that I needed. It took a little more effort, not so much more by the way, but the result was promosing. The main difference is that the time to capture the image was greatly reduced. Also, once you setup the code of DirectShow, the implementation is relatively easy.

I copied a class called Capture.cs to my solution, made a reference to the DirectShow Library 2005 and then used following code to take a snapshot:

Cursor.Current = Cursors.WaitCursor;
// Release any previous buffer
if (m_ip != IntPtr.Zero)
      m_ip = IntPtr.Zero;
            // capture image
m_ip = cam.Click();
Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, 
PixelFormat.Format24bppRgb, m_ip);
pictureBox1.Height = b.Height;
pictureBox1.Width = b.Width;
// If the image is upsidedown
pictureBox1.Image = b;
Cursor.Current = Cursors.Default;

In the default constructor I initialize the camera device and assign it to a second picturebox, so you get video streaming.

this.ApplicationTitle = "Cigarette counter";
const int VIDEODEVICE = 0; 
// zero based index of video capture device to use
const int VIDEOWIDTH = 640; // Depends on video device caps
const int VIDEOHEIGHT = 480; // Depends on video device caps
const int VIDEOBITSPERPIXEL = 24; 
// BitsPerPixel values determined by device

This works great, for now I decided to loose the WIA code and use the DirectShow library.

You can dowload the library and samples here.

I created a simple demo project using directshow, download this here.