目录
开发环境: Visual Studio 2022 .NET Freamwork 4.7.1(这个框架并无特殊要求,都行)
准备工作
因为需要用到Aforge相关程序包,这个需要下载并添加到项目中
这样就好了
组件
需要用到的组件如图所示,例如,链接按钮,命名为button1。
源代码
using AForge.Video.DirectShow;
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using AForge.Video;
using AForge.Controls;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public partial class Form : System.Windows.Forms.Form
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
private FilterInfoCollection videoDevices;//所有摄像设备
private string savePath;
private string folderPath;
private Timer timer;
private DateTime startTime;
private DateTime endTime;
private TimeSpan interval;
private int photoCount=1;
private VideoCaptureDevice videoDevice;//摄像设备
private VideoCapabilities[] videoCapabilities;//摄像头分辨率
public Form()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new Timer();
timer.Tick += new EventHandler(Timer_Tick);
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//得到机器所有接入的摄像设备
if (videoDevices.Count != 0)//读取到摄像设备
{
foreach (FilterInfo device in videoDevices)
{
cboVideo.Items.Add(device.Name);//把摄像设备添加到摄像列表中
}
}
else
{
cboVideo.Items.Add("没有找到摄像头");
}
cboVideo.SelectedIndex = 0;//默认选择第一个
textBox4.Text += folderPath ;
if (videoDevices.Count != 0)//读取到摄像设备
{
//获取摄像头
videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);
GetDeviceResolution(videoDevice);//获得摄像头的分辨率
}
}
private void Timer_Tick(object sender, EventArgs e)
{
// 拍摄照片
Image photo = videoSourcePlayer1.GetCurrentVideoFrame();
pictureBox1.Image = photo;
// 保存照片到指定路径
string savePath = Path.Combine(folderPath, "time lapse "+photoCount.ToString() + ".jpg");
photo.Save(savePath);
photoCount++;
// 检查是否达到结束时间,如果是则停止拍摄
if (DateTime.Now >= endTime)
{
button2_Click(sender,e);
timer.Stop();
videoSourcePlayer1.SignalToStop();
}
}
private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
{
cboResolution.Items.Clear();//清空列表
videoCapabilities = videoCaptureDevice.VideoCapabilities;//设备的摄像头分辨率数组
foreach (VideoCapabilities capabilty in videoCapabilities)
{
//把这个设备的所有分辨率添加到列表
cboResolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
cboResolution.SelectedIndex = 0;//默认选择第一个
}
private void button1_Click(object sender, EventArgs e)
{
if (videoDevice != null)//如果摄像头不为空
{
videoDevice.VideoResolution = videoCapabilities[cboResolution.SelectedIndex];//摄像头分辨率
videoSourcePlayer1.VideoSource = videoDevice;//把摄像头赋给控件
videoSourcePlayer1.Start();//开启摄像头
EnableControlStatus(false);
}
else
{
MessageBox.Show("没有找到摄像头");
}
}
private void EnableControlStatus(bool status)
{
cboVideo.Enabled = status;
cboResolution.Enabled = status;
button1.Enabled = status;
button2.Enabled = !status;
button3.Enabled = status;
}
private void button4_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowser.ShowDialog();
// 检查用户是否选择了文件夹
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath))
{
// 用户选择的文件夹路径
folderPath = folderBrowser.SelectedPath;
// 构造保存路径
// 显示保存成功的消息
MessageBox.Show("路径设置成功!");
textBox4.Text += folderPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
DisConnect();//断开连接
EnableControlStatus(true);
}
private void DisConnect()
{
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.VideoSource = null;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
EnableControlStatus(true);
DisConnect();//关闭并释放
if (timer != null)
{
timer.Stop();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void videoSourcePlayer1_Click(object sender, EventArgs e)
{
}
private async void button5_Click(object sender, EventArgs e)
{
if (folderPath == null)
{
MessageBox.Show("未设置保存路径!点击确定以设置");
DialogResult result = folderBrowser.ShowDialog();
// 检查用户是否选择了文件夹
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath))
{
// 用户选择的文件夹路径
folderPath = folderBrowser.SelectedPath;
// 构造保存路径
textBox4.Text += folderPath;
// 显示保存成功的消息
MessageBox.Show("路径设置成功!");
}
}
// 获取用户输入的开始时间、结束时间和时间间隔
startTime = dateTimePicker2.Value;
endTime = dateTimePicker3.Value;
interval = TimeSpan.FromSeconds((double)numericUpDown1.Value*1000);
// 启动定时器开始拍摄
photoCount = 1;
TimeSpan timeRemaining = startTime - DateTime.Now;
if (timeRemaining.TotalMilliseconds <= 0)
{
timer.Start(); // 启动计时器
}
else
{
// 等待至开始时间
await Task.Delay((int)(startTime - DateTime.Now).TotalMilliseconds);
timer.Start(); // 启动计时器
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
timer.Interval = Convert.ToInt32(numericUpDown1.Value) * 1000;
}
private void button3_Click(object sender, EventArgs e)
{
cboVideo.Items.Clear();//清空列表
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)//读取到摄像设备
{
foreach (FilterInfo device in videoDevices)
{
cboVideo.Items.Add(device.Name);//把摄像设备添加到摄像列表中
}
}
else
{
cboVideo.Items.Add("没有找到摄像头");
}
if (videoDevices.Count != 0)//读取到摄像设备
{
cboVideo.SelectedIndex = 0;//默认选择第一个
//获取摄像头
videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);
GetDeviceResolution(videoDevice);//获得摄像头的分辨率
}
}
private void cboVideo_SelectedIndexChanged(object sender, EventArgs e)
{
// 获取选中的摄像设备
videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);
// 更新cboResolution的选项
GetDeviceResolution(videoDevice);
// 默认选择第一个分辨率
cboResolution.SelectedIndex = 0;
}
private void cboResolution_SelectedIndexChanged(object sender, EventArgs e)
{
videoDevice.VideoResolution = videoCapabilities[cboResolution.SelectedIndex];
}
private void button6_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("未保存数据都将丢失。确定要继续吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
// 根据用户选择执行相应的操作
if (result == DialogResult.OK)
{
// 用户点击了确定按钮,执行相应的操作
timer.Stop();
videoSourcePlayer1.Stop();
DisConnect();
Application.Restart();
}
}
}
}
软件界面
程序压缩包
软件链接:https://pan.baidu.com/s/1Z-G4_CqohhRZVNT1cjJddg?pwd=brhc
提取码:brhc