⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cecamera.cs

📁 This sample demonstrates how to take pictures and videos using the CameraCaptureDialog managed AP
💻 CS
📖 第 1 页 / 共 3 页
字号:
            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 = "Resolution (Width x Height):";
            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, 22);
            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, 22);
            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 = "Video Time Limit:";
            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, 22);
            this.textVideoTimeLimit.TabIndex = 11;
            // 
            // MainForm
            // 
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(169, 180);
            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>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Application.Run(new MainForm());
        }

        /// <summary>
        /// Exit the application.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void MainForm_Closed(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Enable/Disable user input of the textInitialDirectory edit control
        /// for allowing/disallowing the user to specify the initial directory
        /// of the CameraCaptureDialog.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void checkInitialDirectory_CheckStateChanged(object sender, System.EventArgs e)
        {
            this.textInitialDirectory.Enabled = this.checkInitialDirectory.Checked;
        }

        /// <summary>
        /// Enable/Disable user input of the textDefaultFileName edit control
        /// for allowing/disallowing the user to specify the default file name
        /// of the CameraCaptureDialog.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void checkDefaultFileName_CheckStateChanged(object sender, System.EventArgs e)
        {
            this.textDefaultFileName.Enabled = this.checkDefaultFileName.Checked;

            validateFileNameInput();
        }

        /// <summary>
        /// Enable/Disable user input of the textTitle edit control
        /// for allowing/disallowing the user to specify the title
        /// of the CameraCaptureDialog.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void checkTitle_CheckStateChanged(object sender, System.EventArgs e)
        {
            this.textTitle.Enabled = this.checkTitle.Checked;
        }

        /// <summary>
        /// Enable/Disable user input of the textResolutionWidth and textResolutionHeight edit control
        /// for allowing/disallowing the user to specify the resolution width and height
        /// of the CameraCaptureDialog.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void checkResolution_CheckStateChanged(object sender, System.EventArgs e)
        {
            this.textResolutionWidth.Enabled = this.checkResolution.Checked;
            this.textResolutionHeight.Enabled = this.checkResolution.Checked;
        }

        /// <summary>
        /// Enable/Disable user input of the textVideoTimeLimit edit control
        /// for allowing/disallowing the user to specify the video time limit
        /// of the CameraCaptureDialog.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">Event arguments</param>
        private void checkVideoTimeLimit_CheckStateChanged(object sender, EventArgs e)
        {
            this.textVideoTimeLimit.Enabled = this.checkVideoTimeLimit.Checked;
        }

        /// <summary>
        /// Launches Camera using CameraCaptureDialog with specified arguments.
        /// </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;

            // Specify the options as user specified.
            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("Please input a valid resolution width.", this.Text,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    textResolutionWidth.Focus();
                    return;
                }
                if (!ConvertStringToInt(this.textResolutionHeight.Text, ref resolutionHeight))
                {
                    MessageBox.Show("Please input a valid resolution height.", 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("Please input a valid video time limit.", this.Text,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    textVideoTimeLimit.Focus();
                    return;
                }

                cameraCapture.VideoTimeLimit = new TimeSpan(0, 0, videoTimeLimit);
            }

            // Specify capture mode
            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;
            }

            // Specify still quality
            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;
            }

            // Specify video types
            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
            {
                // Displays the "Camera Capture" dialog box
                if (DialogResult.OK == cameraCapture.ShowDialog())
                {
                    string fileName = cameraCapture.FileName;

                    // If it is a video we rename the file so that it has the user entered
                    // default filename and the correct extension.
                    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);
                    }

                    // The method completed successfully.
                    MessageBox.Show("The picture or video has been successfully captured and saved to:\n\n" + fileName,
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
            }
            catch (ArgumentException ex)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -