📄 cecamera.cs
字号:
// textTitle
//
this.textTitle.Location = new System.Drawing.Point(5, 131);
this.textTitle.MaxLength = 64;
this.textTitle.Name = "textTitle";
this.textTitle.Size = new System.Drawing.Size(141, 21);
this.textTitle.TabIndex = 5;
//
// checkResolution
//
this.checkResolution.Location = new System.Drawing.Point(5, 158);
this.checkResolution.Name = "checkResolution";
this.checkResolution.Size = new System.Drawing.Size(154, 16);
this.checkResolution.TabIndex = 6;
this.checkResolution.Text = "指定尺寸(宽 x 高):";
this.checkResolution.CheckStateChanged += new System.EventHandler(this.checkResolution_CheckStateChanged);
//
// textResolutionWidth
//
this.textResolutionWidth.Location = new System.Drawing.Point(5, 180);
this.textResolutionWidth.MaxLength = 10;
this.textResolutionWidth.Name = "textResolutionWidth";
this.textResolutionWidth.Size = new System.Drawing.Size(52, 21);
this.textResolutionWidth.TabIndex = 7;
//
// labelX
//
this.labelX.Location = new System.Drawing.Point(63, 180);
this.labelX.Name = "labelX";
this.labelX.Size = new System.Drawing.Size(24, 16);
this.labelX.Text = "x";
this.labelX.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textResolutionHeight
//
this.textResolutionHeight.Location = new System.Drawing.Point(93, 180);
this.textResolutionHeight.MaxLength = 10;
this.textResolutionHeight.Name = "textResolutionHeight";
this.textResolutionHeight.Size = new System.Drawing.Size(53, 21);
this.textResolutionHeight.TabIndex = 9;
//
// checkVideoTimeLimit
//
this.checkVideoTimeLimit.Location = new System.Drawing.Point(5, 207);
this.checkVideoTimeLimit.Name = "checkVideoTimeLimit";
this.checkVideoTimeLimit.Size = new System.Drawing.Size(115, 19);
this.checkVideoTimeLimit.TabIndex = 10;
this.checkVideoTimeLimit.Text = "视频时间限制:";
this.checkVideoTimeLimit.CheckStateChanged += new System.EventHandler(this.checkVideoTimeLimit_CheckStateChanged);
//
// textVideoTimeLimit
//
this.textVideoTimeLimit.Location = new System.Drawing.Point(5, 232);
this.textVideoTimeLimit.MaxLength = 10;
this.textVideoTimeLimit.Name = "textVideoTimeLimit";
this.textVideoTimeLimit.Size = new System.Drawing.Size(154, 21);
this.textVideoTimeLimit.TabIndex = 11;
//
// MainForm
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(240, 268);
this.Controls.Add(this.checkInitialDirectory);
this.Controls.Add(this.textInitialDirectory);
this.Controls.Add(this.checkDefaultFileName);
this.Controls.Add(this.textDefaultFileName);
this.Controls.Add(this.checkTitle);
this.Controls.Add(this.textTitle);
this.Controls.Add(this.checkResolution);
this.Controls.Add(this.textResolutionWidth);
this.Controls.Add(this.labelX);
this.Controls.Add(this.textResolutionHeight);
this.Controls.Add(this.checkVideoTimeLimit);
this.Controls.Add(this.textVideoTimeLimit);
this.Menu = this.mainMenu;
this.Name = "MainForm";
this.Text = "CECamera";
this.Closed += new System.EventHandler(this.MainForm_Closed);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 程序入口
/// </summary>
static void Main()
{
Application.Run(new MainForm());
}
/// <summary>
/// 程序退出
/// </summary>
private void MainForm_Closed(object sender, System.EventArgs e)
{
Application.Exit();
}
private void checkInitialDirectory_CheckStateChanged(object sender, System.EventArgs e)
{
this.textInitialDirectory.Enabled = this.checkInitialDirectory.Checked;
}
private void checkDefaultFileName_CheckStateChanged(object sender, System.EventArgs e)
{
this.textDefaultFileName.Enabled = this.checkDefaultFileName.Checked;
validateFileNameInput();
}
private void checkTitle_CheckStateChanged(object sender, System.EventArgs e)
{
this.textTitle.Enabled = this.checkTitle.Checked;
}
private void checkResolution_CheckStateChanged(object sender, System.EventArgs e)
{
this.textResolutionWidth.Enabled = this.checkResolution.Checked;
this.textResolutionHeight.Enabled = this.checkResolution.Checked;
}
private void checkVideoTimeLimit_CheckStateChanged(object sender, EventArgs e)
{
this.textVideoTimeLimit.Enabled = this.checkVideoTimeLimit.Checked;
}
/// <summary>
/// 调用照相机
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStart_Click(object sender, System.EventArgs e)
{
CameraCaptureDialog cameraCapture = new CameraCaptureDialog();
cameraCapture.Owner = this;
// Gets or sets the name of the directory for storing the captured image or video file.
//设置路径以便保存捕获到的图像或者视频
if (this.checkInitialDirectory.Checked)
{
cameraCapture.InitialDirectory = this.textInitialDirectory.Text;
}
if (this.checkDefaultFileName.Checked)
{
if (this.menuModeStill.Checked)
{
if (cameraCapture.DefaultFileName != null)
{
// It is necessary to end picture files with ".jpg".
// Otherwise the argument is invalid.
cameraCapture.DefaultFileName = cameraCapture.DefaultFileName + cecameraDefaultPictureExtension;
}
}
else
{
// If it is a video, pass null. This will return a filename with a
// correct extension. Later on we rename the file and keep the extension.
cameraCapture.DefaultFileName = null;
}
}
if (this.checkTitle.Checked)
{
cameraCapture.Title = this.textTitle.Text;
}
if (this.checkResolution.Checked)
{
int resolutionWidth = 0;
int resolutionHeight = 0;
if (!ConvertStringToInt(this.textResolutionWidth.Text, ref resolutionWidth))
{
MessageBox.Show("请输入有效宽度.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textResolutionWidth.Focus();
return;
}
if (!ConvertStringToInt(this.textResolutionHeight.Text, ref resolutionHeight))
{
MessageBox.Show("请输入有效高度.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textResolutionHeight.Focus();
return;
}
cameraCapture.Resolution = new Size(resolutionWidth, resolutionHeight);
}
if (this.checkVideoTimeLimit.Checked)
{
int videoTimeLimit = 0;
if (!ConvertStringToInt(this.textVideoTimeLimit.Text, ref videoTimeLimit))
{
MessageBox.Show("请输入有效的视频时间限制.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textVideoTimeLimit.Focus();
return;
}
//视频的时间最大限制
cameraCapture.VideoTimeLimit = new TimeSpan(0, 0, videoTimeLimit);
}
// 指定捕获模式
if (this.menuModeStill.Checked)
{
cameraCapture.Mode = CameraCaptureMode.Still;//只捕获一张相片
}
else if (this.menuModeVideoOnly.Checked)
{
cameraCapture.Mode = CameraCaptureMode.VideoOnly;//捕获没有声音的视频
}
else if (this.menuModeVideoWithAudio.Checked)
{
cameraCapture.Mode = CameraCaptureMode.VideoWithAudio;//捕获声音和视频
}
// 制定画面质量
if (this.menuStillQualityDefault.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Default;//默认质量
}
else if (this.menuStillQualityLow.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Low;//低质量
}
else if (this.menuStillQualityNormal.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Normal;//正常质量
}
else if (this.menuStillQualityHigh.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.High;//高质量
}
// 制定视频类型
if (this.menuVideoTypesAll.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.All;//所有
}
else if (this.menuVideoTypesStandard.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.Standard;//标准
}
else if (this.menuVideoTypesMessaging.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.Messaging;//较差
}
try
{
// 相机捕获对话框
if (DialogResult.OK == cameraCapture.ShowDialog())
{
string fileName = cameraCapture.FileName;
//如果是一段视频,将使用用户指定的文件名和扩展名进行命名
if (cameraCapture.Mode != CameraCaptureMode.Still)
{
string extension = fileName.Substring(fileName.LastIndexOf("."));
string directory = "";
if (fileName.LastIndexOf("\\") != -1)
{
directory = fileName.Substring(0, fileName.LastIndexOf("\\") + 1);
}
fileName = directory + this.textDefaultFileName.Text + extension;
System.IO.File.Move(cameraCapture.FileName, fileName);
}
MessageBox.Show("相片或者视频已经成功捕获并保存在:\n\n" + fileName,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
}
catch (ArgumentException ex)
{
// 非法变量异常
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
catch (OutOfMemoryException ex)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -