延迟摄影小软件(附源代码及最终程序)

目录

准备工作

组件

源代码

程序压缩包


 

开发环境: Visual Studio 2022 .NET Freamwork 4.7.1(这个框架并无特殊要求,都行)

 

准备工作

因为需要用到Aforge相关程序包,这个需要下载并添加到项目中

fbc64e26bef5448e87beeb6112cb0e1d.png这样就好了

组件

19a79a8499e74e7d8e955869f82fd29c.png

需要用到的组件如图所示,例如,链接按钮,命名为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();
            }
        }
    }
}

软件界面692ffe3142074c65bdf026b506d6c57a.png

程序压缩包

软件链接:https://pan.baidu.com/s/1Z-G4_CqohhRZVNT1cjJddg?pwd=brhc 
提取码:brhc

 

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wen吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值