• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#winformAForge调用视频录制视频拍照及logn4的简单用法(附源代码) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

项目代码地址: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();

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#数据保存到Excel发布时间:2022-07-13
下一篇:
C#一般处理程序发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap