1.項目->添加引用 AForge AForge.Video.DirectShow AForge.Video AForge.Video.FFMPEG AForge.Video.Controls 2.使用AForge.Video.FFMPEG錄制視頻需要以下設置 1)配置管理器->平台改成:x86 2)App.config修改成: 3)複制AForge.NET Framework-2.2.5-(libs only)\Externals\ffmpeg\bin目錄下所有文件至 Projects\WpfApplication3\WpfApplication3\bin\x86\Debug(你的項目Debug目錄下) 3.視頻采集 1)WPF添加VideoSourcePlayer控件,因為是winforms控件,需要用到WindowsFormsHost 2)獲取視頻設備,開啟視頻 private FilterInfoCollection videoDevices; private VideoCaptureDevice div = null; // 設定初始視頻設備 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count > 0) { // 默認設備 div = new VideoCaptureDevice(videoDevices[0].MonikerString); sourcePlayer.VideoSource = div; } sourcePlayer.Start(); 4.抓取圖片 System.Windows.Controls.Image image = new System.Windows.Controls.Image(); image.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( sourcePlayer.GetCurrentVideoFrame().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 5.錄制視頻 1)利用NewFrame事件,兩種方式 一是VideoSourcePlayer控件的NewFrame事件:private void sourcePlayer_NewFrame(object sender, ref Bitmap image) 利用控件生成事件代碼: 注意:private void sourcePlayer_NewFrame(object sender, Bitmap image),參數缺少ref引用,需手動修改 利用代碼控制綁定事件: sourcePlayer.NewFrame += new AForge.Controls.VideoSourcePlayer.NewFrameHandler(sourcePlayer_NewFrame); 錄制完視頻解除綁定: sourcePlayer.NewFrame -= new AForge.Controls.VideoSourcePlayer.NewFrameHandler(sourcePlayer_NewFrame); 二是VideoCaptureDevice對象的NewFrame事件:void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) 利用代碼控制綁定事件: div.NewFrame += new NewFrameEventHandler(videoSource_NewFrame); 錄制完視頻解除綁定: div.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame); 2)在NewFrame事件中,利用VideoFileWriter對象寫入視頻文件 6.WPF下用到winforms控件标簽WindowsFormsHost會遮擋wpf控件顯示問題 代替VideoSourcePlayer控件可以使用WPF标準控件Image,在NewFrame事件中,不斷刷新Image.Source,因為是跨線程控制Image控件的屬性值, 需要利用委托方法的方式刷新Image.Source |
有話要說...