ส่วนแรกลอง preview webcam ด้วย AForge พอคลิกเลือกกล้อง จะแสดงผลภาพจากเว็บแคม
- ใส่ PictureBox และ ComboBox ลงใน Form
- Add Reference ด้วย AForge.Video.dll และ AForge.Video.VideoDirectshow.dll
- Add namespace ด้วย
using AForge.Video.DirectShow;
- กำหนดตัวแปร
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 อีกครั้ง
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();
}
}
พอคลิกเลือกกล้อง กล้องจะเริ่มทำงาน
ไม่มีความคิดเห็น:
แสดงความคิดเห็น