123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- using Microsoft.Win32;
- using PaintDotNet.Camera;
- using PaintDotNet.Camera;
- using PaintDotNet.SystemLayer;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PaintDotNet.ImageCollect
- {
- internal partial class CameraVideoRecording : FloatingToolForm
- {
- /// <summary>
- /// 视频格式
- /// </summary>
- private string[] fmtArray = new string[] { "avi" };
- // 保存路径
- private string m_filePath = Application.StartupPath + "\\Video";
- // 持续时间
- private int m_duration = 0;
- private ICamera m_camera;
- private Timer timer1;
- bool _recording = false;
- bool _done = false;
- DateTime _timeLog;
- string _filename;
- public CameraVideoRecording()
- {
- InitializeComponent();
- InitializeLanguageText();
- InitializeControlData();
- this.Load += (s, e) =>
- {
- m_filePath = Settings.CurrentUser.GetString("CameraRecordPath", "");
- txtSavePath.Text = m_filePath;
- txtPrefix.Text = Settings.CurrentUser.GetString("CameraRecordPrefix", "");
- };
- this.FormClosing += (s, e) =>
- {
- Settings.CurrentUser.SetString("CameraRecordPrefix", txtPrefix.Text);
- Settings.CurrentUser.SetString("CameraRecordPath", txtSavePath.Text);
- };
- }
- /// <summary>
- /// 设置下拉等数据源
- /// </summary>
- private void InitializeControlData()
- {
- this.cmbVideoFormat.Items.AddRange(fmtArray);
- this.cmbVideoFormat.SelectedIndex = 0;
- this.txtSavePath.Text = m_filePath;
- m_camera = CameraManager.CurrentCamera;
- }
- private void btnSavePath_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog folderBroeser = new FolderBrowserDialog();
- if (folderBroeser.ShowDialog() == DialogResult.OK)
- {
- m_filePath = folderBroeser.SelectedPath;
- txtSavePath.Text = m_filePath;
- }
- }
- /// <summary>
- /// 开始录制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (!Directory.Exists(m_filePath))
- {
- Directory.CreateDirectory(m_filePath);
- }
- int bLapse = 1;
- if (rdoS.Checked)
- {
- bLapse = 1000;
- }
- m_duration = (int)(nudDuration.Value * bLapse);
- if (m_duration < 1000)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Therecordingtimeannotbelessthansecond.Text"));
- return;
- }
- StartRecord();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- void StartRecord()
- {
- string prefix = txtPrefix.Text.Trim();
- _filename = m_filePath + "\\" + prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".avi";
- m_camera.RecStart(_filename);
- _recording = true;
- _timeLog = DateTime.Now;
- }
- /// <summary>
- /// 结束录制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnStop_Click(object sender, EventArgs e)
- {
- stopRecording();
- _recording = false;
- }
- private void stopRecording()
- {
- _recording = false;
- _done = true;
- m_camera.RecStop();
- }
- private void CameraVideoRecording_FormClosing(object sender, FormClosingEventArgs e)
- {
- stopRecording();
- }
- private void btnPlay_Click(object sender, EventArgs e)
- {
- if (_filename != null)
- {
- Process.Start(_filename);
- }
- }
- #region 控件
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- // Windows Form Designer generated code
- private void InitializeLanguageText()
- {
- this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
- this.btnPlay.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
- this.btnStop.Text = PdnResources.GetString("Menu.stop.text");
- this.btnStart.Text = PdnResources.GetString("Menu.Started.text");
- this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text");
- this.rdoM.Text = PdnResources.GetString("Menu.millisecond.text");
- this.rdoS.Text = PdnResources.GetString("Menu.second.text");
- this.btnSavePath.Text = PdnResources.GetString("Menu.alter.text");
- this.label4.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Shootingformat.text") + ":";
- this.label3.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.shootingtime.text") + ":";
- this.label2.Text = PdnResources.GetString("Menu.Nameprefix.text") + ":";
- this.label1.Text = PdnResources.GetString("Menu.saveroute.text") + ":";
- this.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Videorecording.text");
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.btnPlay = new System.Windows.Forms.Button();
- this.btnStop = new System.Windows.Forms.Button();
- this.btnStart = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.cmbVideoFormat = new System.Windows.Forms.ComboBox();
- this.rdoM = new System.Windows.Forms.RadioButton();
- this.rdoS = new System.Windows.Forms.RadioButton();
- this.nudDuration = new System.Windows.Forms.NumericUpDown();
- this.btnSavePath = new System.Windows.Forms.Button();
- this.txtPrefix = new System.Windows.Forms.TextBox();
- this.txtSavePath = new System.Windows.Forms.TextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.timer1 = new System.Windows.Forms.Timer(this.components);
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.nudDuration)).BeginInit();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.btnPlay);
- this.groupBox1.Controls.Add(this.btnStop);
- this.groupBox1.Controls.Add(this.btnStart);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(428, 75);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "操作";
- //
- // btnPlay
- //
- this.btnPlay.Enabled = false;
- this.btnPlay.Location = new System.Drawing.Point(340, 26);
- this.btnPlay.Name = "btnPlay";
- this.btnPlay.Size = new System.Drawing.Size(68, 30);
- this.btnPlay.TabIndex = 2;
- this.btnPlay.Text = "播放";
- this.btnPlay.UseVisualStyleBackColor = true;
- this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
- //
- // btnStop
- //
- this.btnStop.Enabled = false;
- this.btnStop.Location = new System.Drawing.Point(266, 26);
- this.btnStop.Name = "btnStop";
- this.btnStop.Size = new System.Drawing.Size(68, 30);
- this.btnStop.TabIndex = 1;
- this.btnStop.Text = "停止";
- this.btnStop.UseVisualStyleBackColor = true;
- this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
- //
- // btnStart
- //
- this.btnStart.Location = new System.Drawing.Point(192, 26);
- this.btnStart.Name = "btnStart";
- this.btnStart.Size = new System.Drawing.Size(68, 30);
- this.btnStart.TabIndex = 0;
- this.btnStart.Text = "开始";
- this.btnStart.UseVisualStyleBackColor = true;
- this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.cmbVideoFormat);
- this.groupBox2.Controls.Add(this.rdoM);
- this.groupBox2.Controls.Add(this.rdoS);
- this.groupBox2.Controls.Add(this.nudDuration);
- this.groupBox2.Controls.Add(this.btnSavePath);
- this.groupBox2.Controls.Add(this.txtPrefix);
- this.groupBox2.Controls.Add(this.txtSavePath);
- this.groupBox2.Controls.Add(this.label4);
- this.groupBox2.Controls.Add(this.label3);
- this.groupBox2.Controls.Add(this.label2);
- this.groupBox2.Controls.Add(this.label1);
- this.groupBox2.Location = new System.Drawing.Point(12, 100);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(428, 249);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "设置";
- //
- // cmbVideoFormat
- //
- this.cmbVideoFormat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this.cmbVideoFormat.FormattingEnabled = true;
- this.cmbVideoFormat.Location = new System.Drawing.Point(266, 173);
- this.cmbVideoFormat.Name = "cmbVideoFormat";
- this.cmbVideoFormat.Size = new System.Drawing.Size(95, 20);
- this.cmbVideoFormat.TabIndex = 10;
- //
- // rdoM
- //
- this.rdoM.AutoSize = true;
- this.rdoM.Location = new System.Drawing.Point(324, 127);
- this.rdoM.Name = "rdoM";
- this.rdoM.Size = new System.Drawing.Size(47, 16);
- this.rdoM.TabIndex = 9;
- this.rdoM.Text = "毫秒";
- this.rdoM.UseVisualStyleBackColor = true;
- //
- // rdoS
- //
- this.rdoS.AutoSize = true;
- this.rdoS.Checked = true;
- this.rdoS.Location = new System.Drawing.Point(260, 127);
- this.rdoS.Name = "rdoS";
- this.rdoS.Size = new System.Drawing.Size(35, 16);
- this.rdoS.TabIndex = 8;
- this.rdoS.TabStop = true;
- this.rdoS.Text = "秒";
- this.rdoS.UseVisualStyleBackColor = true;
- //
- // nudDuration
- //
- this.nudDuration.Location = new System.Drawing.Point(177, 125);
- this.nudDuration.Maximum = new decimal(new int[] {
- 276447231,
- 23283,
- 0,
- 0});
- this.nudDuration.Minimum = new decimal(new int[] {
- 1,
- 0,
- 0,
- 0});
- this.nudDuration.Name = "nudDuration";
- this.nudDuration.Size = new System.Drawing.Size(72, 21);
- this.nudDuration.TabIndex = 7;
- this.nudDuration.Value = new decimal(new int[] {
- 1,
- 0,
- 0,
- 0});
- //
- // btnSavePath
- //
- this.btnSavePath.Location = new System.Drawing.Point(367, 32);
- this.btnSavePath.Name = "btnSavePath";
- this.btnSavePath.Size = new System.Drawing.Size(55, 25);
- this.btnSavePath.TabIndex = 6;
- this.btnSavePath.Text = "更改";
- this.btnSavePath.UseVisualStyleBackColor = true;
- this.btnSavePath.Click += new System.EventHandler(this.btnSavePath_Click);
- //
- // txtPrefix
- //
- this.txtPrefix.Location = new System.Drawing.Point(103, 75);
- this.txtPrefix.Name = "txtPrefix";
- this.txtPrefix.Size = new System.Drawing.Size(258, 21);
- this.txtPrefix.TabIndex = 5;
- //
- // txtSavePath
- //
- this.txtSavePath.Location = new System.Drawing.Point(103, 32);
- this.txtSavePath.Name = "txtSavePath";
- this.txtSavePath.ReadOnly = true;
- this.txtSavePath.Size = new System.Drawing.Size(258, 21);
- this.txtSavePath.TabIndex = 4;
- this.txtSavePath.Text = "r";
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Location = new System.Drawing.Point(15, 176);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(65, 12);
- this.label4.TabIndex = 3;
- this.label4.Text = "拍摄格式:";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(15, 127);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(65, 12);
- this.label3.TabIndex = 2;
- this.label3.Text = "拍摄时间:";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(15, 80);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(65, 12);
- this.label2.TabIndex = 1;
- this.label2.Text = "名称前缀:";
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(15, 35);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(65, 12);
- this.label1.TabIndex = 0;
- this.label1.Text = "保存路径:";
- //
- // timer1
- //
- this.timer1.Enabled = true;
- this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
- //
- // CameraVideoRecording
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(459, 373);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
- this.Location = new System.Drawing.Point(0, 0);
- this.Name = "CameraVideoRecording";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
- this.Text = "视频录制";
- this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraVideoRecording_FormClosing);
- this.Controls.SetChildIndex(this.groupBox1, 0);
- this.Controls.SetChildIndex(this.groupBox2, 0);
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox2.PerformLayout();
- ((System.ComponentModel.ISupportInitialize)(this.nudDuration)).EndInit();
- this.ResumeLayout(false);
- }
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button btnPlay;
- private System.Windows.Forms.Button btnStop;
- private System.Windows.Forms.Button btnStart;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.ComboBox cmbVideoFormat;
- private System.Windows.Forms.RadioButton rdoM;
- private System.Windows.Forms.RadioButton rdoS;
- private System.Windows.Forms.NumericUpDown nudDuration;
- private System.Windows.Forms.Button btnSavePath;
- private System.Windows.Forms.TextBox txtPrefix;
- private System.Windows.Forms.TextBox txtSavePath;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label1;
- #endregion
- private void timer1_Tick(object sender, EventArgs e)
- {
- btnStart.Enabled = !_recording;
- btnStop.Enabled = _recording;
- btnPlay.Enabled = _done && !_recording;
- if (_recording && (DateTime.Now - _timeLog).TotalMilliseconds > m_duration)
- {
- stopRecording();
- }
- }
- }
- }
|