2013年6月18日 星期二

【AForge.NET】使用AForge擷取視訊畫面並可調整解析度

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;

namespace CaptureWebCam
{
    public partial class CaptureWebCam : Form
    {
        private VideoCaptureDevice m_cam = null;
        private FilterInfoCollection USB_cams = null;
        public CaptureWebCam()
        {
            InitializeComponent();
        }
        private void CaptureWebCam_Load(object sender, EventArgs e)
        {
            DefaultCamFilter();//檢查可用的視訊裝置
        }

        private void CaptureWebCam_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
        private void DefaultCamFilter()
        {
            try
            {
                cbxCam.Items.Clear();
                USB_cams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (USB_cams.Count == 0)
                    throw new ApplicationException();
                foreach (FilterInfo device in USB_cams)
                {
                    //將所有視訊裝置列入COMBOBOX
                    cbxCam.Items.Add(device.Name);
                }
                cbxCam.SelectedIndex = 0;
            }
            catch
            {
                MessageBox.Show("No Video Device");
            }
        }
        private void CamSupportedFrameSizes(VideoCaptureDevice videoSource)
        {
            this.Cursor = Cursors.WaitCursor;
            cbxCamResolution.Items.Clear();
            VideoCapabilities[] videoCapabilities;
            try
            {
                videoCapabilities = videoSource.VideoCapabilities;
                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    if (!cbxCamResolution.Items.Contains(capabilty.FrameSize))
                    {
                        //將該視訊裝置的所有支援解析度列入COMBOBOX
                        cbxCamResolution.Items.Add(capabilty.FrameSize);                    
                    }
                }
                if (videoCapabilities.Length == 0)
                {                    
                    MessageBox.Show("Not supported any resolution");
                }
                cbxCamResolution.SelectedIndex = 0;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        
        //當改變視訊裝置時,重新整理支援視訊解析度
        private void cbxCam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (USB_cams.Count != 0)
            {
                m_cam = new VideoCaptureDevice(USB_cams[cbxCam.SelectedIndex].MonikerString);
                CamSupportedFrameSizes(m_cam);
            }
        }
        private void btnCamStart_Click(object sender, EventArgs e)
        {
            if (m_cam.IsRunning)
                m_cam.Stop();
            try
            {
                //新增視訊裝置
                m_cam = new VideoCaptureDevice(USB_cams[cbxCam.SelectedIndex].MonikerString);
                //選擇視訊解析度
                m_cam.DesiredFrameSize = (Size)cbxCamResolution.SelectedItem;
                //開始執行視訊擷取
                m_cam.NewFrame += new NewFrameEventHandler(m_cam_NewFrame);
            }
            finally
            {
                m_cam.Start();
            }
            
        }
        private void btnCamStop_Click(object sender, EventArgs e)
        {
            if (m_cam != null)
                if (m_cam.IsRunning)
                    m_cam.Stop();
        }
   
        private void m_cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                //複製視訊擷取的影像
                Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
                if (bmp != null)
                {
                    //將擷取影像畫在PICTUREBOX上
                    Graphics graphics = pcbMainFrame.CreateGraphics();
                    graphics.DrawImage(bmp, 0, 0, pcbMainFrame.Width, pcbMainFrame.Height);
                    graphics.Dispose();
                }
            }
            catch
            {                
            }
        }       
    }
}



0 意見:

張貼留言