Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Webcamera, Multithreading and VFW

, 15 Feb 2008
An article on webcamera frame-grabbing in a multi-thread environment
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using SevenZ.WebCamera;
using SevenZ.ImageProcessing;

namespace SevenZ.WebCam
   public partial class FormMain : Form
      int time = 0;
      bool bRunning = false;

      WebCameraDevice camDevice;

      public FormMain()
         WebCameraDeviceManager camManager = new WebCameraDeviceManager();
         cmbDevices.SelectedIndex = 0;

      void camDevice_OnCameraFrame(object sender, WebCameraEventArgs e)
         // img processing: 20-30 ms
         ImageProcessing.Filters.Flip(e.Frame, false, true);

         pictureBox.Image = chkGrayScale.Checked ?
            ImageProcessing.Filters.ToGrayScale(e.Frame) :

         this.Text = "FPS: " + (1000 / (Environment.TickCount - time)).ToString();
         time = Environment.TickCount;

      private void btnStart_Click(object sender, EventArgs e)
         if (bRunning = !bRunning)
            camDevice = new WebCameraDevice(320, 200, 25, cmbDevices.SelectedIndex, this.Handle.ToInt32());
            camDevice.OnCameraFrame += new WebCameraFrameDelegate(camDevice_OnCameraFrame);
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            btnDeviceProperties.Enabled = true;
            btnStart.Text = "Stop";
            btnDeviceProperties.Enabled = false;
            btnStart.Text = "Start";


      private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
         if(camDevice != null)

      private void btnDevice_Click(object sender, EventArgs e)

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.


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


About the Author

Zaur Nasibov
Software Developer
Finland Finland
I'm a Master degree student, studying at the University of Joensuu, Finland.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 16 Feb 2008
Article Copyright 2007 by Zaur Nasibov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid