AForge Video
简介
使用AForge Video进行录像
界面
运行
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;//using
using System.IO;//Path、Directory用到
using System.Timers;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Controls;namespace AforgeVideo
{public partial class Form1 : Form{private delegate void MyDelegateUI();//多线程问题public FilterInfoCollection videoDevices = null; //摄像头设备public VideoCaptureDevice cam1 = null; //视频的来源选择public VideoFileWriter videoWriter = null;//写入到视频public Bitmap bm1 = null;Boolean is_record_video = false;Boolean ifCam1 = false;int i = 1; //摄像头数int hour = 0;int tick_num = 0;int width = 640; //录制视频的宽度int height = 480; //录制视频的高度int fps = 20; //正常速率,fps越大速率越快,相当于快进System.Timers.Timer timer_count;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){//初始化按钮状态labelScreen.Visible = false;btnPauseVideo.Enabled = false;btnStartVideo.Enabled = false;btnStopVideo.Enabled = false;btnTakePic.Enabled = false;//设置视频编码格式comboBox_videoecode.Items.Add("MPEG");comboBox_videoecode.Items.Add("WMV");comboBox_videoecode.Items.Add("FLV");comboBox_videoecode.Items.Add("H263p");comboBox_videoecode.Items.Add("MSMPEG4v3");comboBox_videoecode.SelectedIndex = 0;//设置视频来源try{// 枚举所有视频输入设备videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices.Count == 0)throw new ApplicationException(); //没有找到摄像头设备foreach (FilterInfo device in videoDevices){comboBox_camera.Items.Add(device.Name);textBoxC.AppendText("摄像头" + i + "初始化完毕..." + "\n");textBoxC.ScrollToCaret();i++;}//comboBox_camera.SelectedIndex = 0; //注释掉,选择摄像头来源的时候才会才会触发显示摄像头信息}catch (ApplicationException){comboBox_camera.Items.Add("No local capture devices");videoDevices = null;MessageBox.Show("没有找到摄像头设备", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}//秒表timer_count = new System.Timers.Timer(); //实例化Timer类,设置间隔时间为1000毫秒;timer_count.Elapsed += new System.Timers.ElapsedEventHandler(tick_count); //到达时间的时候执行事件;timer_count.AutoReset = true; //设置是执行一次(false)还是一直执行(true);timer_count.Interval = 1000;}private void comboBox_camera_SelectedIndexChanged(object sender, EventArgs e){cam1 = new VideoCaptureDevice(videoDevices[comboBox_camera.SelectedIndex].MonikerString);//cam1.DesiredFrameSize = new System.Drawing.Size(width, height);//cam1.DesiredFrameRate = fps;cam1.NewFrame += new NewFrameEventHandler(Cam_NewFrame1);cam1.Start();labelScreen.Text = "连接中...";labelScreen.Visible = true;ifCam1 = true;//激活初始化按钮状态btnTakePic.Enabled = true;btnPauseVideo.Enabled = true;btnStartVideo.Enabled = true;btnStopVideo.Enabled = true;}private void Cam_NewFrame1(object obj, NewFrameEventArgs eventArgs){pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();bm1 = (Bitmap)eventArgs.Frame.Clone();if (is_record_video){videoWriter.WriteVideoFrame(bm1);}}//计时器响应函数public void tick_count(object source, System.Timers.ElapsedEventArgs e){tick_num++;int temp = tick_num;int sec = temp % 60;int min = temp / 60;if (min%60==0){hour = min / 60;min = 0;}else{min = min - hour * 60;}String tick = hour.ToString() + ":" + min.ToString() + ":" + sec.ToString();MyDelegateUI d = delegate{this.labelTime.Text = tick;};this.labelTime.Invoke(d);}private void btnStartVideo_Click(object sender, EventArgs e){//创建一个视频文件String video_format = comboBox_videoecode.Text.Trim(); //获取选中的视频编码String picName = GetVideoPath() + "\\" + DateTime.Now.ToString("yyyyMMdd HH-mm-ss");if (-1 != video_format.IndexOf("MPEG")){picName = picName + ".avi";}else if (-1 != video_format.IndexOf("WMV")){picName = picName + ".wmv";}else{picName = picName + ".mkv";}textBoxC.AppendText("Video保存地址:" + picName + "\n");textBoxC.ScrollToCaret();timer_count.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;labelScreen.Text = "REC";textBoxC.AppendText("录制中...\n");is_record_video = true;videoWriter = new VideoFileWriter();if (cam1.IsRunning){if (-1 != video_format.IndexOf("MPEG")){videoWriter.Open(picName, width, height, fps, VideoCodec.MPEG4);}else if (-1 != video_format.IndexOf("WMV")){videoWriter.Open(picName, width, height, fps, VideoCodec.WMV1);}else{videoWriter.Open(picName, width, height, fps, VideoCodec.Default);}}else{MessageBox.Show("没有视频源输入,无法录制视频。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}private void btnStopVideo_Click(object sender, EventArgs e){labelScreen.Visible = false;this.is_record_video = false;this.videoWriter.Close();this.timer_count.Enabled = false;tick_num = 0;textBoxC.AppendText("录制停止!\n");textBoxC.ScrollToCaret();}private void btnExit_Click(object sender, EventArgs e){if (this.videoWriter == null){//MessageBox.Show("videoWriter对象未实例化。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//return;videoWriter = new VideoFileWriter();}if (this.videoWriter.IsOpen){MessageBox.Show("视频流还没有写完,请点击结束录制。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);return;}cam1.SignalToStop();cam1.WaitForStop();Hide();Close();Dispose();}private void btnPauseVideo_Click(object sender, EventArgs e){if (this.btnPauseVideo.Text.Trim() == "暂停录像"){is_record_video = false;labelScreen.Visible = false;btnPauseVideo.Text = "恢复录像";timer_count.Enabled = false; //暂停计时return;}if (this.btnPauseVideo.Text.Trim() == "恢复录像"){is_record_video = true;labelScreen.Visible = true;btnPauseVideo.Text = "暂停录像";timer_count.Enabled = true; //恢复计时}}private void btnTakePic_Click(object sender, EventArgs e){if (ifCam1){String filepath = GetImagePath() + "\\"+DateTime.Now.ToString("yyyyMMdd HH-mm-ss") + ".bmp";bm1.Save(filepath);textBoxC.AppendText("摄像头1保存:" + filepath + "\n");textBoxC.ScrollToCaret();}elseMessageBox.Show("摄像头没有运行", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);}//获取Video相对路径?private String GetVideoPath(){String personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)+ Path.DirectorySeparatorChar.ToString() + "MyVideo";if (!Directory.Exists(personImgPath)){Directory.CreateDirectory(personImgPath);}return personImgPath;}//获取Img相对路径?private String GetImagePath(){String personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)+ Path.DirectorySeparatorChar.ToString() + "MyImages";if (!Directory.Exists(personImgPath)){Directory.CreateDirectory(personImgPath);}return personImgPath;}}
}
代码
问题
在使用AForge这个软件过程中需要的不仅仅是将Release文件夹下对应的lib添加到项目的引用中,在进行视频压缩编码的时候需要将External文件夹下的相关lib添加到程序运行的Debug目录下
在使用的时候会遇到这个错误“混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况”的错误。这是因为.net框架不兼容的问题,AForge使用的比较老好像是2.0的。。。-_-||。所以需要在App.config对其进行配置,使其对前版本的兼容,配置如下(这是我添加的配置):
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/><supportedRuntime version="v2.0.50727"/></startup>
【1】在writer.Close()执行前,视频帧都是在内存中的,虽然通过writer.Open()在硬盘上也生成了文件,但是文件大小为0.。 若果长时间录制不间断,内存就会溢出。除非录制一段时间后,就把内存中的视频保存到硬盘一次。
【2】图像大小不完全显示
参考
【1】基于AForge的C#摄像头视频录制 - CSDN博客
http://blog.csdn.net/m_buddy/article/details/62417912
【2】AForge.net 使用之录像拍照功能实现 - CSDN博客
http://blog.csdn.net/chenhongwu666/article/details/41965801
【3】C# 调用AForge类库操作摄像头 - CSDN博客
http://blog.csdn.net/chenhongwu666/article/details/40594365
【4】C#实现录音录像录屏源码 - zhuweisky - 博客园
http://www.cnblogs.com/zhuweisky/p/3593917.html
【5】AForge.net 使用之录像拍照功能实现 - ching126 - 博客园
http://www.cnblogs.com/ching2009/p/4168000.html
【6】C# 下写入视频的简单实现 - CSDN博客
http://blog.csdn.net/whw8007/article/details/21232737
【7】AForge.Video.FFMPEG库使用注意事项 - liang12360640的专栏 - CSDN博客
http://blog.csdn.net/liang12360640/article/details/46044763
【8】C#:AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。解决办法收集(转载) - 焦涛 - 博客园
http://www.cnblogs.com/Joetao/articles/5779328.html
AForge Video相关推荐
- c#屏幕录制(经典)(含源码和AForge.Video.FFMPEG.DLL)及填坑办法
一直觉得.net在多媒体处理方面渣得不行.最近需要做一个摄像头的程序,为了方便,用了AForge这个开源项目.AForge项目中有AForge.Video和AForge.Video. DirectSh ...
- AForge.Video.FFMPEG库使用注意事项
AForge.Video.FFMPEG库几个注意事项 使用场景:用AForge.Video.FFMPEG.VideoFileWriter对象,将图片压缩成avi视频,压缩算法mpeg4,帧率5,多线程 ...
- AForge “未能加载文件或程序集“AForge.Video.FFMPEG.dll”或它的某一个依赖项。找不到指定的模块”
问题:未能加载文件或程序集"AForge.Video.FFMPEG.dll"或它的某一个依赖项.找不到指定的模块. 1.Aforge.Net子项目有个AForge.Video.VF ...
- C#机器学习插件 ---- AForge.NET
目录 简介 主要架构 特点 学习之旅 简介 AForge.NET是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能, ...
- WPF中通过AForge实现USB摄像头拍照
最近由于某种原因呢,需要做一下拍照的功能,本来我纠结到底用AForge类库还是用WPFMediaKit.dll ,不过后来看网上说WPFMediaKit.dll 是截图而AForge是直接通过摄像头拍 ...
- AForge.net简介和认识
AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域. 这个框架由一系列的类库组成.主要包括 ...
- C# 调用AForge类库操作摄像头
如有雷同,不胜荣幸,若转载,请注明 最近做项目需要操作摄像头,在网上百度了很多资料,很多都是C#调用window API 发送SendMessage,实现操作摄像头,但是C#调用window API的 ...
- AForge.net 使用之录像拍照功能实现
AForge.net 使用之录像拍照功能实现 最近使用aforge.net拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...
- c# 利用AForge.NET组件操作摄像头
AForge.NET是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,机器 ...
- 基于AForge的C#摄像头视频录制
1. 概述 最近搞华为的CDN(-_-||-_-||-_-||),写东西的时间比较少了.最近由于兴趣学习了下在C#上使用AForge录制摄像头视频并压缩编码.总体上来说这个第三方.net视觉开发库还是 ...
最新文章
- 使用Typescript的巧妙React上下文技巧-不是Redux
- MySQL数据库的数据类型decimal详解
- chrome插件 实现微博言论监控
- cte公用表表达式_CTE SQL删除; 在SQL Server中删除具有公用表表达式的数据时的注意事项
- docker增加端口映射_docker配置lamp环境笔记
- (openssh、telnet、vsftpd、nfs、rsync、inotify、samba)
- 回来不是为了留下,而是为了重新出发 -- 生死阅读影评
- .Top域名:新顶级域名还原互联网安全发展
- understanding OpenGL
- 单片机c语言信号灯定时编程,单片机定时器控制交通灯程序1
- php xss漏洞扫描工具,XSS漏洞扫描器工具:XSpear
- 服务器系统更新后找不到硬盘,重装win10系统找不到硬盘完美解决方法
- 游戏对战平台研究终结篇【转】
- Android Studio报错Using insecure protocols with repositories
- 凸优化理论基础1--仿射集
- web(Response、ServletContext)
- 云平台是什么、什么是云、云平台的分类、主流公有云平台有哪些、云的三种服务、PaaS、SaaS、IaaS
- FileDownload文件的下载
- 2017.10.19 測試總結并今日總結
- 智能温度、电压监测系统