Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

WebCam Fast Image Capture Service using WIA

Rate me:
Please Sign up or sign in to vote.
4.57/5 (57 votes)
17 Aug 2006CPOL4 min read 726.8K   83.9K   222   93
Grab frames from any webcam, transfer frames using WebServices, super easy and super fast
Sample Image - WebCamService.jpg

Update #1

  • I have added Directx to obtain cameras that did not show up using WIA. You may download the source code from Sourceforge.
  • Minimized delay at startup.
  • Even though it was pretty fast to grab a frame, it is now a little faster.
  • I have also added the ability to continuously grab frames from camera (delegates and timers).
  • I have not documented most of my code, but I will very soon (Sorry).
  • For the next update, you should be able to multicast images on your network. I am actually testing it right now. I have to say it works really well.

Please let me know if you run into any bugs or if you have any questions. I would be happy to help you.

Introduction

For a long time, I have been looking for a good way to capture images without too much trouble. All you need to do is call one method to get the image.

I have used Windows Image Acquisition (WIA) to capture images. WIA provides a great deal of compatibility with webcams already running on Microsoft Windows XP.

This is my first posting on The Code Project, please do not hesitate to comment or give any suggestions. I would be very thankful if you provide any feedback.

WIA

Improved Image Capture Technologies: WIA

Windows XP supports still-image devices through Windows Image Acquisition (WIA), which uses the Windows Driver Model (WDM) architecture. WIA provides robust communication between applications and image-capture devices, allowing you to capture images efficiently and transfer them to your computer for editing and use.

WIA supports small computer system interface (SCSI), IEEE 1394, USB, and serial digital still image devices. Support for infrared, parallel, and serial still-image devices—which are connected to standard COM ports—comes from the existing infrared, parallel, and serial interfaces. Image scanners and digital cameras are examples of still image devices. WIA also supports Microsoft DirectShow®-based webcams and digital video (DV) camcorders to capture frames from video.

Windows Image Acquisition Architecture

WIA architecture is both an Application Programming Interface (API) and a Device Driver Interface (DDI). The WIA architecture includes components provided by the software and hardware vendor, as well as by Microsoft. Figure 1 below illustrates the WIA architecture.

Figure 1: The components of WIA architecture.
Figure 1: The components of WIA architecture.

Click here to see full-sized image.

The DLL Fix

I have included all the required references in the source code. You don't need to do this, but if you wish to know how you may read this section.

Reference

You want to add a reference to Microsoft Windows Image Acquisition type library.

Reference2

You also want to add a reference to WiaVideo Type Library. However, after some trouble I read about a bug and its fix that occurs while importing the type library.

Use ILDASM to dump the type library Interop.WIAVIDEOLib.dll, then use Notepad to replace occurrences of valuetype _RemotableHandle& to native int. Then compile the iL dump file you fixed using ILASM in the CMD ilasm /DLL WIAVIDEOLib.il. Add a reference to the new DLL and you are set to go.

The Code

Please keep in mind that the first image you take will initialize the camera. The initialization process takes a few seconds. You may notice the Join(3000) I added when activating the webcam. I added the join in order to avoid exceptions and allow enough time for the camera to do its thing. It was necessary to use the join. I think this delay is because USB protocol is slow or maybe it is just my camera. After the first image is taken, images start flying so fast it is going to look like a live video feed!!

C#
//WebCam.cs
using System;
using System.Collections.Generic;
using System.Text;
using WIALib;
using WIAVIDEOLib;
using System.Runtime.InteropServices;
namespace WebCamLibrary
{
    /// <SUMMARY>
    /// This Class will initialize the camera using Windows Image Acquisition (WIA).
    /// Also, provides methods to view all connected cameras, capture single frame.
    /// </SUMMARY>
    public class WebCam
    {
        private static WebCam WebCamObj;
        /// <SUMMARY>
        ///  variables needed 
        /// </SUMMARY>
        private WIAVIDEOLib.WiaVideo WiaVideoObj;
        private WIALib.Wia WiaObj;
        private WIALib.DeviceInfo DefaultDevice;
        private WIALib.DeviceInfo[] Devices;
        private bool DeviceIsOpen;
        /// <SUMMARY>
        /// Initialize The WebCam Class
        /// </SUMMARY>
        private WebCam()
        {
            DeviceIsOpen = false;
            Initialize();
            SetDefaultDevice();
            OpenDefaultDevice();
        }
        /// <SUMMARY>
        /// Please use this method to create a WebCam Object.
        /// </SUMMARY>
        /// <RETURNS>WebCam Object</RETURNS>
        public static WebCam NewWebCam()
        {
            if (WebCam.WebCamObj == null)
                WebCam.WebCamObj = new WebCam();

            return WebCam.WebCamObj;
        }
        private void Initialize()
        {
            if (this.WiaObj == null)
            {
                this.WiaObj = new WiaClass();
            }
            if (this.WiaVideoObj == null)
            {
                this.WiaVideoObj = new WiaVideoClass();
                this.WiaVideoObj.ImagesDirectory = System.IO.Path.GetTempPath();
            }
        }
        /// <SUMMARY>
        /// Use it to close any open resources.
        /// </SUMMARY>
        public void CloseResources()
        {
            try
            {
                if (WiaObj != null)
                    Marshal.FinalReleaseComObject(WiaObj);
                if (WiaVideoObj != null)
                    Marshal.FinalReleaseComObject(WiaVideoObj);
                if (DefaultDevice != null)
                    Marshal.FinalReleaseComObject(DefaultDevice);
                if (Devices != null)
                    Marshal.FinalReleaseComObject(Devices);

                DeviceIsOpen = false;
                GC.Collect();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }
        private void SetDefaultDevice()
        {
            CloseDefaultDevice();

            System.Collections.Generic.List<WIALIB.DEVICEINFOCLASS> devs = 
                                        new List<DEVICEINFOCLASS>();
            WIALib.Collection devsCol = 
                            this.WiaObj.Devices as WIALib.CollectionClass;
            foreach (object obj in devsCol)
            {
                WIALib.DeviceInfoClass dev = (WIALib.DeviceInfoClass)
                  System.Runtime.InteropServices.Marshal.CreateWrapperOfType(
                  obj, typeof(WIALib.DeviceInfoClass));
                if (dev.Type.Contains("Video") == true)
                {
                    devs.Add(dev);
                }
                else
                {
                    Marshal.FinalReleaseComObject(obj);
                    Marshal.FinalReleaseComObject(dev);
                }
            }
            Devices = devs.ToArray();
            if (Devices.Length > 0)
            {
                DefaultDevice = Devices[0];
            }
            else
            {
                throw new Exception("No Cameras Detected");
            }

            Marshal.FinalReleaseComObject(devsCol);
            GC.ReRegisterForFinalize(devs);
        }
        /// <SUMMARY>
        /// Get Connected Cameras
        /// </SUMMARY>
        /// <RETURNS>string[] contains names of connected cameras</RETURNS>
        public string[] GetConnectedCameras()
        {
            string[] devs = new string[this.Devices.Length];
            for (int i = 0; i < devs.Length; i++)
            {
                devs[i] = this.Devices[i].Name;
            }
            return devs;
        }
        private void CloseDefaultDevice()
        {
            if (this.DeviceIsOpen == false)
                return;
            try
            {
                if (this.WiaVideoObj != null)
                {
                    this.WiaVideoObj.DestroyVideo();
                    Marshal.FinalReleaseComObject(this.DefaultDevice);
                    DeviceIsOpen = false;
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            finally
            {
                DeviceIsOpen = false;
            }
        }
        /// <SUMMARY>
        /// Use this method to set the camera to capture 
        /// from if you have more than one camera.
        /// </SUMMARY>
        /// <param name="name" />Name of the camera device.
        /// Get name from GetConnectedCameras() </param />
        /// <RETURNS></RETURNS>
        public bool SetDefaultDevice(string name)
        {
            CloseDefaultDevice();

            for (int i = 0; i < Devices.Length; i++)
            {
                if (Devices[i].Name.Equals(name))
                {
                    DefaultDevice = Devices[i];
                    return true;
                }
            }
            return false;
        }
        private bool OpenDefaultDevice()
        {
            CloseDefaultDevice();

            try
            {
                this.WiaVideoObj.PreviewVisible = 0;
                this.WiaVideoObj.CreateVideoByWiaDevID(this.DefaultDevice.Id,
                                                       IntPtr.Zero, 0, 0);
                System.Threading.Thread.CurrentThread.Join(3000);
                this.DeviceIsOpen = true;
                this.WiaVideoObj.Play();
                System.Threading.Thread.CurrentThread.Join(3000);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
                CloseDefaultDevice();
                return false;
            }

            return true;
        }
        /// <SUMMARY>
        /// Use it to grab a frame. Use System.IO.MemoryStream to load image
        /// </SUMMARY>
        /// <RETURNS>byte array of the captured image</RETURNS>
        public byte[] GrabFrame()
        {
            string imagefile;
            this.WiaVideoObj.TakePicture(out imagefile);
            return ReadImageFile(imagefile);
        }
        private byte[] ReadImageFile(string img)
        {
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(img);
            byte[] buf = new byte[fileInfo.Length];
            System.IO.FileStream fs = new System.IO.FileStream(img, 
                      System.IO.FileMode.Open,System.IO.FileAccess.Read);
            fs.Read(buf, 0, buf.Length);
            fs.Close();
            fileInfo.Delete();
            GC.ReRegisterForFinalize(fileInfo);
            GC.ReRegisterForFinalize(fs);
            return buf;
        }
    }
}
//WebCamService.cs
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "www.codeproject.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebCamService : System.Web.Services.WebService
{
    private WebCamLibrary.WebCam cam;

    public WebCamService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        cam = WebCamLibrary.WebCam.NewWebCam();      
    }
    [WebMethod]
    public byte[] GrabFrame() {
        return cam.GrabFrame();
    }
    [WebMethod]
    public string[] GetConnectedCameras()
    {
        return cam.GetConnectedCameras();
    } 
}

Future Features

  • Redo this article, I don't think I did a very good job because its 6:00 am -> bedtime.
  • I would like use my other project to be able to broadcast images over the network(multicasting).
  • Look into possible compression technologies.
  • I think it would be great to transfer Images over UDP using smallest headers possible.
  • WebControl for Handheld devices to view camera images over the Web.

Notes

Fixed article layout

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer TECO GROUP - Jundi Electrical Industry
United States United States
if you have any questions please send an email :

ojundi @ [%s].info

Oz [Jundi]

Comments and Discussions

 
Bugdoes not work Pin
halotron27-Aug-19 0:26
halotron27-Aug-19 0:26 
Questionwebcam script(s) Pin
Joachim Wegener4-Jun-17 9:19
Joachim Wegener4-Jun-17 9:19 
QuestionHow do I get images on OS startup Pin
Member 1138819914-Apr-15 1:26
Member 1138819914-Apr-15 1:26 
QuestionNOT WORK Pin
Member 1085208330-May-14 2:30
Member 1085208330-May-14 2:30 
QuestionBad Sample ... Avoid Pin
Steveo1237-Mar-14 9:36
Steveo1237-Mar-14 9:36 
SuggestionThank you for your article! I would like to recommend my new Camera_Net Library Pin
free5lot29-Dec-13 21:15
free5lot29-Dec-13 21:15 
QuestionSample Not Working Pin
Member 1018922415-Aug-13 21:14
Member 1018922415-Aug-13 21:14 
AnswerRe: Sample Not Working Pin
Shambhoo kumar16-Sep-13 20:07
professionalShambhoo kumar16-Sep-13 20:07 
Questionconvert code 2 vb.net Pin
NanoParedaz5-Aug-12 19:11
NanoParedaz5-Aug-12 19:11 
QuestionHow can i get CAPTURE event via WIA ? Pin
peter lee sh22-Apr-12 19:11
peter lee sh22-Apr-12 19:11 
Questionusing in asp.net Pin
yasoo8317-Jan-12 5:28
yasoo8317-Jan-12 5:28 
Questioni need a help Pin
sdurgeshs10-Sep-11 3:08
sdurgeshs10-Sep-11 3:08 
QuestionWIA compatible webcams? Pin
jeremylow18-Mar-11 23:23
jeremylow18-Mar-11 23:23 
AnswerRe: WIA compatible webcams? Pin
Albert van Peppen3-Mar-12 8:02
professionalAlbert van Peppen3-Mar-12 8:02 
GeneralClass not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) at WIAVIDEOLib.IWiaVideo.CreateVideoByName Pin
olgaquecano30-Sep-10 0:42
olgaquecano30-Sep-10 0:42 
GeneralExcellent Pin
tandahunter28-Apr-10 21:52
tandahunter28-Apr-10 21:52 
GeneralHelp!!! Pin
dustinkbaker21-Dec-09 5:45
dustinkbaker21-Dec-09 5:45 
AnswerRe: Help!!! Pin
Member 370561816-Mar-11 11:24
Member 370561816-Mar-11 11:24 
Generalgrab the frame to disk and read....... not good idea Pin
reborn_zhang5-Dec-09 23:54
reborn_zhang5-Dec-09 23:54 
GeneralMy vote of 1 Pin
criollo02-Dec-09 18:47
criollo02-Dec-09 18:47 
GeneralMy vote of 1 Pin
Priyank Bolia13-Nov-09 4:57
Priyank Bolia13-Nov-09 4:57 
QuestionIt seems many errors there? Pin
satishgv19851-Apr-09 0:11
satishgv19851-Apr-09 0:11 
GeneralInside a thread Pin
brousse12326-Mar-09 2:01
brousse12326-Mar-09 2:01 
GeneralRe: Inside a thread Pin
brousse12326-Mar-09 8:15
brousse12326-Mar-09 8:15 
QuestionError using the code "unable to connect to remote server" Pin
xicorete23-Feb-09 5:56
xicorete23-Feb-09 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.