C# 启动Windows摄像头类 使用Aforge.net
2017-07-30 17:09
330 查看
/// <summary> /// 启动摄像头类 需要引用using AForge.Video; using AForge.Video.DirectShow; /// </summary> public class Video { ///---声明变量 private FilterInfoCollection USE_Webcams = null; private VideoCaptureDevice Camera = null; public event EventHandler<EventArgs> Realtime_Img_Generation; public Video() { ///---实例化对象 USE_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice); ///---摄像头数量大于0 if (USE_Webcams.Count > 0) { ///---实例化对象 Camera = new VideoCaptureDevice(USE_Webcams[0].MonikerString); ///---绑定事件 Camera.NewFrame += new NewFrameEventHandler(Cam_NewFrame); } } [System.Runtime.InteropServices.DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr hObject); private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs) { try { System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => { var frame = eventArgs.Frame.Clone() as Bitmap; ///---throw new NotImplementedException(); IntPtr ptr = frame.GetHbitmap(); BitmapSource bitmapImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); //release resource DeleteObject(ptr); frame.Dispose(); OnRealtime_Img_Generation(eventArgs, bitmapImage); })); } catch (Exception ex) { return; } } private void OnRealtime_Img_Generation(EventArgs e, BitmapSource bitMap) { EventHandler<EventArgs> handler = Realtime_Img_Generation; if (handler != null) handler(bitMap, e); } public void StartCamera() { Task.Factory.StartNew(() => { if (Camera != null) { Camera.Start(); } }); } public void StopCamera() { Task.Factory.StartNew(() => { if (Camera != null) { Camera.Stop(); } }); } }
用法示例
private void Window_Loaded(object sender, RoutedEventArgs e) { video = new Video(); video.StartCamera(); video.Realtime_Img_Generation += Video_Realtime_Img_Generation; } private void Video_Realtime_Img_Generation(object sender, EventArgs e) { System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => { var bitmap = sender as BitmapSource; img.Source = bitmap; })); }
相关文章推荐
- 使用 Visual C# .Net 确定 Windows 版本
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 使用C#代码安装、启动、停止、卸载Windows service(不使用InstallUtil.exe)
- 子句判断、启动强度和去模糊化--AForge.NET框架的使用(三)
- 摘:如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- C#使用Aforge.net framework采集摄像头视频
- 续【C# 以管理员方式启动Winform,进而使用管理员控制Windows Service】
- vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动
- C#学习之 调用 AForge.NET Framework 启动摄像头
- C#学习之 调用 AForge.NET框架启动摄像头抓拍
- .Net C# Windows Service在服务器无法启动,错误 193:0xc1
- c# 使用AForge.NET操作摄像头操作代码
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页?
- 使用 Visual C# .NET 检查 Windows 版本
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动
- 70-316 使用Microsoft Visual C# .NET and Microsoft Visual Studio .NET 开发及实现基于Windows的应用程序 考点整理
- 使用C# .NET 将结构数组绑定到 Windows 窗体的方法
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页