【问题标题】:Get Webcam stream using Aforge.NET in C# and WPF在 C# 和 WPF 中使用 Aforge.NET 获取网络摄像头流
【发布时间】:2011-05-26 12:25:32
【问题描述】:

我想使用我的摄像头捕捉网络摄像头源。为此,我使用了 2 个参考:AForge.Video.dllAForge.Video.DirectShow.dll

Here's我发现的一个sn-p:

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 中工作时,我使用了this

总结一下我的代码目前的样子。

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

【问题讨论】:

    标签: .net multithreading silverlight asynchronous aforge


    【解决方案1】:

    Edit1:查看我的blogpost关于同一主题的详细说明。


    我使用Dispatcher 类作为互斥体修复了错误:

    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 没有任何下降。

    【讨论】:

      【解决方案2】:

      如果您想支持 Silverlight,无论是 Web 版、独立版还是 WP7,都不应从 WPF 开始,因为 Silverlight 中缺少 WPF 的许多功能。

      这是 Silverlight 4+ 教程:

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

      【讨论】:

      • 嘿。谢谢...到目前为止,我正在处理的应用程序需要大量的图像处理。所以我使用 Aforge 来做同样的事情(即捕获视频和所有内容)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多