can you tell me which bits to extract?
Main Topics
Browse All TopicsThis is probably fairly simple, but I cant find an answer for this question anywhere.
I have a standard cheapo usb webcam and I need to capture a bitmap image from it. I have found quite a few solutions for integrating a webcam, but none that can dump it to a bitmap image.
I need this solution to work with any usb webcam (if possible), so WIA is not an answer I don't believe.
Basically, my app needs to capture the bitmap of what it sees, and then I will do a number of imaging processes on it.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sure,
assuming you've downloaded the source -
The solution is made up of three projects - dshow, motion and vfw
keep the dshow project, maybe compile it to a dll for portability - should create this if you build it anyway.
In the motion project there is a folder called VideoSource which has all the classes for accessing input etc, i seperated this and compiled it to a dll also, you don't need to that you can just copy the whole directory.
To see how it all goes together look in the CaptureDevice form where you will see it enumerates over all attached devices, if you have one webcam plugged in it'll be the first one, so use
device = filters[0].MonikerString; //this is from the okay button click handler
The code i used (sorry if this is a bit disjointed, spent a while finding my code) is
field: private IVideoSource m_cam;
private void InitialiseWebCam()
{
//only carry this out if a camera hasn't already been set up
if (m_cam == null)
{
FilterCollection filters;
try
{
//get all of the devices attached
filters = new FilterCollection(FilterCat
//get the first item in the collection of devices
Filter webCam = filters[0];
String deviceName = webCam.MonikerString;
//instantiate the web cam
m_cam = new CaptureDevice();
//bind to the device
m_cam.VideoSource = deviceName;
}
catch
{
MessageBox.Show("No webcams found");
Application.Exit();
}
}
}
then to start it call the start method of m_cam, you'll need to create an event handler for the m_cam.NewFrame event (this is how you get the bitmap) :
m_cam.NewFrame += new CameraEventHandler(OnFirst
hope this helps, let me know if you need more info, it may take me a while to look through the code but i don't mind.
Hey, I've got a problem. This is the code I have:
using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using dshow;
using VideoSource;
using dshow.Core;
namespace IDAPT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Initialise the webcam
InitialiseWebCam();
}
// The camera/video source object
private IVideoSource m_cam;
private void OnNewFrame(object sender, CameraEventArgs e)
{
this.pctTemp.Image = e.Bitmap;
}
private void InitialiseWebCam()
{
//only carry this out if a camera hasn't already been set up
if (m_cam == null)
{
FilterCollection filters;
try
{
//get all of the devices attached
filters = new FilterCollection(FilterCat
//get the first item in the collection of devices
Filter webCam = filters[0];
String deviceName = webCam.MonikerString;
//instantiate the web cam
m_cam = new CaptureDevice();
//bind to the device
m_cam.VideoSource = deviceName;
// Set the new frame event
m_cam.NewFrame += new CameraEventHandler(OnNewFr
}
catch
{
MessageBox.Show("No webcams found");
Application.Exit();
}
}
}
private void pctTemp_Click(object sender, EventArgs e)
{
// Start the capture
m_cam.Start();
}
}
}
When I click the picture (just a temp way of starting the capture) it runs for about 5 seconds, then closes the form and highlights the " Application.Run(new Form1()); " in Program.cs.
The error is "Parameter is not valid."
System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.get_W
at System.Drawing.Image.get_S
at System.Windows.Forms.Pictu
at System.Windows.Forms.Pictu
at System.Windows.Forms.Pictu
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at IDAPT.Program.Main() in C:\Documents and Settings\Dan\My Documents\Visual Studio 2005\Projects\IDAPT\IDAPT\
at System.AppDomain.nExecuteA
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
So it'll be the Image = e.Bitmap.
In my code i only really displayed the image for a preview, it was for an eye tracking system, to do this i just drew directly onto the form. I do remember having a similar problem, but because i didn't really need to display the frames for too long (only during calibration) i managed to avoid it. Soloutions i tried that worked were ( i hope i remember correctly)
1) surrounding the this.pctTemp.Image = e.Bitmap; with a try catch so it doesn't bail out
2) checking whether e.Bitmap is null before assigning
3) cloning e.Bitmap before passing it to Image
4) using Monitors around the e.Bitmap to lock it before using it
It may be worth looking at the original code from the motion tracking project, look at the CameraWindow class, in particular the Paint method. In his code he is using a Camera instance instead of the IVideoSource, the Camera class exposes a last frame property, although i think i had the same problems with this as IVideoSource
Business Accounts
Answer for Membership
by: Babycorn-StarfishPosted on 2007-06-12 at 10:45:50ID: 19268241
Hi,
/cs/media/ Motion_Det ection.asp
sorry its a link but it's a pretty decent one
http://www.codeproject.com
download the code and look at the dshow stuff, it taps into DirectX (DirectShow). Also look at the Videosource stuff.
hope this helps