10

カメラを使用して Web カメラ フィードをキャプチャしたいと考えています。そのために、 と の 2 つの参照を使用していAForge.Video.dllます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で作業しているので、これを使用しました

要約すると、現在の私のコードは次のようになります。

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

2 に答える 2

7

Edit1:詳細な説明については、同じトピックに関する私のブログ投稿をご覧ください。


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 が低下することなく良好なパフォーマンスが得られます。

于 2011-05-30T11:11:59.827 に答える
1

Silverlightをサポートする場合は、Web、スタンドアロン、またはWP7のいずれであっても、WPFの多くの機能がSilverlightに欠けているため、WPFから始める必要はありません。

これがSilverlight4+チュートリアルです。

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

于 2011-05-29T07:35:49.753 に答える