วันจันทร์ที่ 22 เมษายน พ.ศ. 2556

Part 1 เปิดเว็บแคม

โปรเจค โปรแกรมถ่ายรูปเก็บไว้ดูเล่น
ส่วนแรกลอง preview webcam ด้วย AForge พอคลิกเลือกกล้อง จะแสดงผลภาพจากเว็บแคม
  • ใส่ PictureBox และ ComboBox ลงใน Form
image
  • Add Reference ด้วย AForge.Video.dll และ AForge.Video.VideoDirectshow.dll
  • Add namespace ด้วย
using AForge.Video;
using AForge.Video.DirectShow;

  • กำหนดตัวแปร
VideoCaptureDevice videoSource;
FilterInfoCollection videoDevices;

private void Form1_Load(object sender, EventArgs e)
        {
           // enumerate video devices            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo info in videoDevices)
            {
                comboBox1.Items.Add(info.Name);
            }  
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                       if(videoSource != null)
            {
                videoSource.Stop();
            }
            // create video source
            videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
//หลังเครื่องหมาย = จะมีข้อความขึ้นอย่างนี้ แล้วก้อกด TAB code จะมาเอง จากนั้นกด TAB อีกครั้ง
image
            videoSource.Start();
        }

       void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
//มันขึ้นมาหลังกด TAB ครั้งที่ 2        {
           Bitmap b = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = b;                     
        }

        private void Form1_FormCloesd(object sender, FormClosedEventArgs e)
        {
           if (videoSource != null)
            {
                videoSource.Stop();
            }
        } 

พอคลิกเลือกกล้อง กล้องจะเริ่มทำงาน


ไม่มีความคิดเห็น:

แสดงความคิดเห็น