Click here to Skip to main content
15,885,823 members
Articles / Desktop Programming / Windows Forms

Versatile WebCam C# library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (137 votes)
23 Feb 2015CPOL9 min read 1.1M   85K   444  
Easy to use C# WebCam library that's not plagued with weird DLL references or PInvoke calls
using System;
using System.ComponentModel.Composition;
using Touchless.Vision.Camera.Configuration;
using Touchless.Vision.Contracts;

namespace Touchless.Vision.Camera
{
    [Export(typeof(IFrameSource))]
    public class CameraFrameSource : IFrameSource
    {
        public event Action<IFrameSource, Frame, double> NewFrame;
        
        public bool IsCapturing { get; private set; }

        private Camera _camera;
        public Camera Camera
        {
            get { return _camera; }

            internal set
            {
                if (_camera != value)
                {
                    bool restart = IsCapturing;
                    if (IsCapturing)
                    {
                        StopFrameCapture();
                    }

                    _camera = value;

                    if (restart)
                    {
                        StartFrameCapture();
                    }
                }
            }
        }

        [ImportingConstructor]
        public CameraFrameSource([Import(ExportInterfaceNames.DefaultCamera)] Camera camera)
        {
            if (camera == null) throw new ArgumentNullException("camera");

            this.Camera = camera;
        }

        public void StartFrameCapture()
        {
            if (!IsCapturing && this.Camera != null)
            {
                this.Camera.OnImageCaptured += OnImageCaptured;
                this.Camera.StartCapture();
                IsCapturing = true;
            }
        }

        public void StopFrameCapture()
        {
            if (IsCapturing)
            {
                this.Camera.StopCapture();
                this.Camera.OnImageCaptured += OnImageCaptured;
                this.IsCapturing = false;
            }
        }

        private void OnImageCaptured(object sender, CameraEventArgs e)
        {
            if (IsCapturing && this.NewFrame != null)
            {
                var frame = new Frame(e.Image);
                this.NewFrame(this, frame, e.CameraFps);
            }
        }

        public string Name
        {
            get { return "Touchless Camera Frame Source"; }
        }

        public string Description
        {
            get { return "Retrieves frames from Camera"; }
        }

        public bool HasConfiguration
        {
            get { return true; }
        }


        public System.Windows.UIElement ConfigurationElement
        {
            get
            {
                return new CameraFrameSourceConfigurationElement(this);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Chief Technology Officer
United States United States
If you liked this article, consider reading other articles by me. For republishing article on other websites, please contact me by leaving a comment.

Comments and Discussions