2012-02-20 35 views
0

我想集成攝像頭,並使用Aforge.NET框架。我需要什麼樣的控制才能顯示攝像頭視頻。我的簡單程序將獲得網絡攝像機,將它放在組合框中,當我點擊「確定」時,我可以看到相機正在運行,但我沒有看到視頻。 C#/ WPF我需要什麼控制才能顯示攝像頭的視頻?

`

<Window x:Class="MyWebCam.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="425"> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="4*"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 

     <Image Grid.ColumnSpan="2" x:Name="videoPlayer" Margin="15"></Image> 
      <ComboBox Grid.Row="1" Grid.ColumnSpan="2" x:Name="cbxDevices" MinWidth="150" Margin="15" VerticalAlignment="Center" HorizontalAlignment="left" SelectionChanged="cbxDevices_SelectionChanged" /> 
    <Button Grid.Column="1" Grid.Row="1" x:Name="btnOk" Content="OK" MinWidth="100" Margin="15" Click="btnOk_Click" /> 
    </Grid> 
</Window> 

`

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using AForge; 
using AForge.Imaging; 
using AForge.Video; 
using AForge.Video.VFW; 
using AForge.Video.DirectShow; 
using AForge.Controls; 
using System.Drawing; 
using AForge.Vision.Motion; 
using AForge.Vision; 

namespace MyWebCam 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     //opened video source 
     private VideoCaptureDevice videoSource = null; 

     //motion detector 
     MotionDetector detector = new MotionDetector(
      new TwoFramesDifferenceDetector(), 
      new MotionAreaHighlighting()); 

     FilterInfoCollection videoDevices; 
     private bool deviceExist = false; 
     private string device; 

     //video device 
     public string VideoDevice 
     { 
      get { return device; } 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 
      getCamList(); 
     } 

     private void getCamList() 
     { 
      try 
      { 
       videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
       cbxDevices.Items.Clear(); 
       if (videoDevices.Count == 0) 
       { 
        throw new ApplicationException(); 
       } 
       deviceExist = true; 
       foreach (FilterInfo device in videoDevices) 
       { 
        cbxDevices.Items.Add(device.Name); 
       } 
       cbxDevices.SelectedIndex = 0; //make first cam default 
      } 
      catch (ApplicationException) 
      { 
       deviceExist = false; 
       cbxDevices.Items.Add("No webcams on your system"); 
      } 
     } 

     private void cbxDevices_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      device = videoDevices[cbxDevices.SelectedIndex].MonikerString; 
      if (cbxDevices.SelectedIndex != -1) 
      { 
       videoSource = new VideoCaptureDevice(videoDevices[cbxDevices.SelectedIndex].MonikerString); 
       videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 

      } 
     } 
     private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) 
     { 
      // get new frame 
      Bitmap video = (Bitmap)eventArgs.Frame; 
      // process the frame 
      videoPlayer.Source = video; //this is where i'm having the problem 

     } 

     private void btnOk_Click(object sender, RoutedEventArgs e) 
     { 
      videoSource.Start(); 
     } 
    } 
} 

回答

2

這裏沒有內置到WPF中。但我可以推薦耶利米莫里爾的WPF MediaKit,你可以在http://wpfmediakit.codeplex.com/

VideoCaptureElement在工具箱找到可以從任何捕捉源(其中包括任何網絡攝像頭)到WPF渲染視頻。

相關問題