Get Webcam stream using Aforge.NET in C# and WPF
Asked Answered
G

2

10

I want to capture a webcam feed using my camera. For that I am using the 2 references: AForge.Video.dll and AForge.Video.DirectShow.dll.

Here's a snippet I found:

public FilterInfoCollection CamsCollection;
public VideoCaptureDevice Cam = null;

void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{   
  frameholder.Source = (Bitmap)eventArgs.Frame.Clone(); 
  /* ^
   * Here it cannot convert implicitly from System.Drawing.Bitmap to
   * System.Windows.Media.ImageSource
   */

}

private void startcam_Click(object sender, RoutedEventArgs e)
{
  CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);

  Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString);
  Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
  Cam.Start();
}

private void stopcam_Click(object sender, RoutedEventArgs e)
{
  Cam.Stop();
}

}

They use a PictureBox to display the frames. As I am working in WPF, I used this

To sum up here's what my code looks like currently.

public FilterInfoCollection CamsCollection;
public VideoCaptureDevice Cam = null;


void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{

    System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();


    BitmapImage bi = new BitmapImage();
    bi.BeginInit ();

    MemoryStream ms = new MemoryStream ();

    imgforms.Save(ms, ImageFormat.Bmp);

    ms.Seek(0, SeekOrigin.Begin);
    bi.StreamSource  = ms;
    frameholder.Source = bi; 
   /* ^ runtime error here because `bi` is occupied by another thread.
    */
    bi.EndInit();
}

private void startcam_Click(object sender, RoutedEventArgs e)
{

    CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);

    Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString);
    Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
    Cam.Start();
}

private void stopcam_Click(object sender, RoutedEventArgs e)
{
    Cam.Stop();
}
Gerous answered 26/5, 2011 at 12:25 Comment(0)
G
7

Edit1: for a detailed explanation view my blogpost on the same topic.


I fixed the error using the Dispatcher class as a mutex:

void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {

        System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone(); 

        BitmapImage bi = new BitmapImage(); 
        bi.BeginInit(); 

        MemoryStream ms = new MemoryStream(); 
        imgforms.Save(ms, ImageFormat.Bmp); 
        ms.Seek(0, SeekOrigin.Begin); 

        bi.StreamSource = ms; 
        bi.EndInit();

        //Using the freeze function to avoid cross thread operations 
        bi.Freeze();

        //Calling the UI thread using the Dispatcher to update the 'Image' WPF control         
        Dispatcher.BeginInvoke(new ThreadStart(delegate
        {
            frameholder.Source = bi; /*frameholder is the name of the 'Image' WPF control*/
        }));     

    }

Now it runs as expected and I get good performance without any drop in the fps.

Gerous answered 30/5, 2011 at 11:11 Comment(0)
S
1

If you want to support Silverlight, be it for web or standalone or WP7, you shouldn't start with WPF, as many features from WPF are lacking in Silverlight.

Here is a Silverlight 4+ tutorial:

http://www.silverlightshow.net/items/Capturing-the-Webcam-in-Silverlight-4.aspx

Subtle answered 29/5, 2011 at 7:35 Comment(1)
hey. Thanks... As of now I will be requiring lots of image processing for the application that I am working on. So I am using Aforge for the same(i.e for capturing the video and all)Gerous

© 2022 - 2024 — McMap. All rights reserved.