C# 启动Windows摄像头类 使用Aforge.net
来源:互联网 发布:linux mv 整个文件夹 编辑:程序博客网 时间:2024/06/06 20:58
/// <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; })); }
阅读全文
0 0
- C# 启动Windows摄像头类 使用Aforge.net
- C#学习之 调用 AForge.NET Framework 启动摄像头
- C#学习之 调用 AForge.NET框架启动摄像头抓拍
- C#使用Aforge.net framework采集摄像头视频
- c# 使用AForge.NET操作摄像头操作代码
- c# 利用AForge.NET组件操作摄像头
- c# 利用AForge.NET组件操作摄像头
- C# 使用AForge调用笔记本摄像头拍照
- C# 调用AForge类库操作摄像头
- C# AForge 关闭摄像头
- C# 利用AForge.NET 调用电脑摄像头进行拍照
- C#使用aforge框架打开摄像头(续)
- 在WPF中使用AForge.net控制摄像头拍照
- Aforge.net摄像头旋转代码
- Aforge.net类库调用摄像头拍照(C#)
- 使用AForge设置摄像头参数
- AForge类库调用摄像头
- C#简单的Aforge调用摄像头
- 2017 Multi-University Training Contest
- Hdu2184汉诺塔VIII
- HDU 1008 Elevator
- javascript 常用函数
- 文章标题
- C# 启动Windows摄像头类 使用Aforge.net
- BroadcastReceiver Hook
- Sublime Text3 如何安装、删除及更新插件
- H
- java:字符型数组转换为字符串;字符串转换为字符型数组
- 欢迎使用CSDN-markdown编辑器
- hdu 5672 String (尺取法)
- git(六)-标签与diff
- HTTPS和HTTP的区别