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

Part 3 Capture ภาพจาก webcam แล้วบันทึกเป็นไฟล์ภาพ

image
  • เพิ่ม namespace ใหม่ ด้วย
using System.IO;
  • เพิ่ม Button (ในนี้คือปุ่ม Shutter) และ เพิ่ม SaveFileDialog ลงใน Form
  • เพิ่มตัวแปรใหม่
Bitmap cap;
  • จากอันเดิม เพิ่ม Code ดังนี้
private void Form1_Load(object sender, EventArgs e)
       {
           videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
          
button1.Enabled = false; //ไม่ให้ ปุ่ม Shutter ใช้การได้           foreach (FilterInfo info in videoDevices)
           {
               comboBox1.Items.Add(info.Name);
           }
       }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != "")
            {
                button1.Enabled = true;
           
}//เมื่อเลือกกล้องแล้ว  ให้ปุ่ม Shutter ใช้การได้            if(videoSource != null)
            {
                videoSource.Stop();
            }

        ……………………….
         }
                  ………………………
  • เพิ่ม Code ส่วน save file ภาพ หลังจากกดปุ่ม Shutter
private void button1_Click(object sender, EventArgs e)                
        {
            saveFileDialog1.Filter = "JPEG(*.jpg)|*.jpg|png(*.png)|*.png|Bitmap(*.bmp)|*.bmp";             videoSource.Stop(); // กล้องหยุด            pictureBox1.Image = cap;
           if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {                         
                cap.Save(saveFileDialog1.FileName);

            }
                System.Threading.Thread.Sleep(500);
                videoSource.Start(); // กล้องทำงานต่อ       

            }   

Part 2 แสดง DateTime ในภาพ


ต่อจากส่วนที่แล้ว ตอนนี้เราจะแสดงตัวเลขของ วัน/เดือน/ปี เวลา ที่เดินไปเรื่อยๆ 

ซึ่งพอ run จะปรากฎพร้อมกับการแสดงผลจาก webcam ใน PictureBox

image
เพิ่ม Code ส่วนนี้ลงไป
void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap b = (Bitmap)eventArgs.Frame.Clone();
           
            DateTime now = DateTime.Now;
            Graphics g =Graphics.FromImage(b);

           // paint current time            SolidBrush brush = new SolidBrush(Color.Red);
            g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
            brush.Dispose();

            g.Dispose();
            cap = b;
            pictureBox1.Image = b;

       }

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();
            }
        } 

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