在C#和WPF中使用Aforge.NET获取网络摄像头流 [英] Get Webcam stream using Aforge.NET in C# and WPF

查看:621
本文介绍了在C#和WPF中使用Aforge.NET获取网络摄像头流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相机捕获网络摄像头.为此,我使用了两个引用:AForge.Video.dllAForge.Video.DirectShow.dll.

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.

这是我发现的一个片段:

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();
}

}

他们使用PictureBox来显示框架.在WPF中工作时,我使用了

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();
}

推荐答案

Edit1 :有关详细说明,请查看我的

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

我使用Dispatcher类作为互斥锁解决了该错误:

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*/
        }));     

    }

现在,它可以按预期运行,并且在不降低fps的情况下,我可以获得良好的性能.

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

这篇关于在C#和WPF中使用Aforge.NET获取网络摄像头流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆