■ 웹 카메라 사용하기

------------------------------------------------------------------------------------------------------------------------


TestSolution.zip


[TestProject 프로젝트]

 

MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

using TestLibrary;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Class

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 콤보 박스 항목 - ComboBoxItem

 

        /// <summary>

        /// 콤보 박스 항목

        /// </summary>

        private class ComboBoxItem

        {

            //////////////////////////////////////////////////////////////////////////////////////////////////// Field

            ////////////////////////////////////////////////////////////////////////////////////////// Private

 

            #region Field

 

            /// <summary>

            /// 비디오 캡처 장치

            /// </summary>

            private readonly VideoCaptureDevice videoCaptureDevice;

 

            #endregion

 

            //////////////////////////////////////////////////////////////////////////////////////////////////// Property

            ////////////////////////////////////////////////////////////////////////////////////////// Public

 

            #region 비디오 캡처 장치 - VideoCaptureDevice

 

            /// <summary>

            /// 비디오 캡처 장치

            /// </summary>

            public VideoCaptureDevice VideoCaptureDevice

            {

                get

                {

                    return this.videoCaptureDevice;

                }

            }

 

            #endregion

 

            //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

            ////////////////////////////////////////////////////////////////////////////////////////// Public

 

            #region 생성자 - ComboBoxItem(videoCaptureDevice)

 

            /// <summary>

            /// 생성자

            /// </summary>

            /// <param name="videoCaptureDevice">비디오 캡처 장치</param>

            public ComboBoxItem(VideoCaptureDevice videoCaptureDevice)

            {

                this.videoCaptureDevice = videoCaptureDevice;

            }

 

            #endregion

 

            //////////////////////////////////////////////////////////////////////////////////////////////////// Method

            ////////////////////////////////////////////////////////////////////////////////////////// Public

 

            #region 문자열 구하기 - ToString()

 

            /// <summary>

            /// 문자열 구하기

            /// </summary>

            /// <returns>문자열</returns>

            public override string ToString()

            {

                return this.videoCaptureDevice.Name;

            }

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load                                                 += Form_Load;

            this.videoCaptureDeviceComboBox.SelectedIndexChanged += videoCaptureDeviceComboBox_SelectedIndexChanged;

            this.startButton.Click                               += startButton_Click;

            this.stopButton.Click                                += stopButton_Click;

            this.imageButton.Click                               += imageButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            foreach(VideoCaptureDevice videoCaptureDevice in this.webCameraControl.GetVideoCaptureDeviceList())

            {

                this.videoCaptureDeviceComboBox.Items.Add(new ComboBoxItem(videoCaptureDevice));

            }

 

            if(this.videoCaptureDeviceComboBox.Items.Count > 0)

            {

                this.videoCaptureDeviceComboBox.SelectedItem = this.videoCaptureDeviceComboBox.Items[0];

            }

        }

 

        #endregion

        #region 비디오 캡처 장치 콤보 박스 선택 인덱스 변경시 처리하기 - videoCaptureDeviceComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 비디오 캡처 장치 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void videoCaptureDeviceComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            SetButtonEnabled();

        }

 

        #endregion

        #region 시작하기 버튼 클릭시 처리하기 - startButton_Click(sender, e)

 

        /// <summary>

        /// 시작하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void startButton_Click(object sender, EventArgs e)

        {

            ComboBoxItem item = (ComboBoxItem)this.videoCaptureDeviceComboBox.SelectedItem;

 

            try

            {

                this.webCameraControl.StartCapture(item.VideoCaptureDevice);

            }

            finally

            {

                SetButtonEnabled();

            }

        }

 

        #endregion

        #region 중단하기 버튼 클릭시 처리하기 - stopButton_Click(sender, e)

 

        /// <summary>

        /// 중단하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void stopButton_Click(object sender, EventArgs e)

        {

            this.webCameraControl.StopCapture();

 

            SetButtonEnabled();

        }

 

        #endregion

        #region 이미지 버튼 클릭시 처리하기 - imageButton_Click(sender, e)

 

        /// <summary>

        /// 이미지 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void imageButton_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();

 

            saveFileDialog.Filter = "비트맵 이미지|*.bmp";

            saveFileDialog.Title  = "이미지 저장";

 

            if(saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                this.webCameraControl.GetCurrentImage().Save(saveFileDialog.FileName);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 버튼 이용 가능 여부 설정하기 - SetButtonEnabled()

 

        /// <summary>

        /// 버튼 이용 가능 여부 설정하기

        /// </summary>

        private void SetButtonEnabled()

        {

            this.startButton.Enabled = this.videoCaptureDeviceComboBox.SelectedItem != null;

            this.stopButton.Enabled  = this.webCameraControl.IsCapturing;

            this.imageButton.Enabled = this.webCameraControl.IsCapturing;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker