项目代码地址:https://gitee.com/LIALL/aforge_video_recording.git
废话就不说了直接上手,展示图如下:
1.引用AForge组件,
nuget 方式 引用如下组件
视频写入引入
偷懒的话 可以将debug下的aforge组件复制过去引用,部分代码如下:
private void Form1_Load(object sender, EventArgs e) { GraphicsPath gp = new GraphicsPath(); Rectangle rectg = new Rectangle( new Point( this.GbxVideo.Location.X, this.GbxVideo.Location.Y), new Size(GbxVideo.Width-50, GbxVideo.Height-50)); gp.AddEllipse(rectg); GbxVideo.Region = new Region(gp);
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0) { MessageBox.Show("请连接视频!","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (videoDevices.Count > 0) {
for (int n = 0; n < videoDevices.Count; n++) { ComboxVideo.Items.Add(videoDevices[0].Name); } } ComboxVideo.Text = videoDevices[0].Name; ComBoxframe.SelectedText = "20";
} VideoCaptureDevice _videoCaptureDevice; public void StartCameras() {
_videoCaptureDevice = new VideoCaptureDevice(videoDevices[0].MonikerString) { DesiredFrameSize = new Size(320, 240),//可做页面设置 DesiredFrameRate = 20
}; videoSourcePlayer1.VideoSource = _videoCaptureDevice; videoSourcePlayer1.Start();
timervideo.Enabled = true; } Thread _mythread; private void btnstartCameras_Click(object sender, EventArgs e) { try { btnstartCameras.Enabled = false; btnstopCameras.Enabled = true; StartCameras(); LogHelper.WriteLog(typeof(Form1), "开始录制视频"); string path = Application.StartupPath + "\\Video"; if (!File.Exists(path)) {
Directory.CreateDirectory(path); } string VideoPath = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff") + "_sp_.avi"; writer = new AVIWriter(); writer.Open(VideoPath, 320, 240); //可设置 _mythread = new Thread(new ThreadStart(TestStart1)); _mythread.Start(); } catch (Exception ex) { LogHelper.WriteLogError(typeof(Form1), ex); } } public void TestStart() {
MicroTimer _microTimer = new MicroTimer(); _microTimer.MicroTimerElapsed += new MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent1); _microTimer.Interval = 49000; _microTimer.Enabled = true; // Thread.Sleep(15000); //录制时间为15秒 _microTimer.Enabled = false; } MicroTimer _microTimer = new MicroTimer(); public void TestStart1() {
// MicroTimer _microTimer = new MicroTimer(); _microTimer.MicroTimerElapsed += new MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent1); _microTimer.Interval = 49000; _microTimer.Enabled = true;
} Bitmap bmp1 = null;
private void OnTimedEvent1(object sender, MicroTimerEventArgs timerEventArgs) { try { writer.AddFrame(bmp1); } catch (Exception ex) {
LogHelper.WriteLogError(typeof(Form1), ex); } }
private void btnstopCameras_Click(object sender, EventArgs e) { if (videoSourcePlayer1.IsRunning) { if (_mythread.IsAlive) _mythread.Abort();
timervideo.Enabled = false; _microTimer.Enabled = false; videoSourcePlayer1.SignalToStop(); videoSourcePlayer1.WaitForStop(); writer.Close(); writer.Dispose();
btnstartCameras.Enabled= true; btnstopCameras.Enabled = false; }
}
private void btnexit_Click(object sender, EventArgs e) { if (videoSourcePlayer1.IsRunning) { MessageBox.Show("请先停止录制视频","提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.Dispose();
|
请发表评论