Kod Örneği
using AForge.Video; using AForge.Video.DirectShow; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource; private void btnCapture_Click(object sender, EventArgs e) { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0) { MessageBox.Show("Kamera bulunamadı."); return; } videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(Video_NewFrame); videoSource.Start(); } private void Video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); // Kameradan gelen görüntüyü PictureEdit'e aktar pictureEdit1.Image = bitmap; // Kamerayı durdur (tek kare için) videoSource.SignalToStop(); videoSource.NewFrame -= Video_NewFrame; }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (videoSource != null && videoSource.IsRunning) { videoSource.SignalToStop(); videoSource.WaitForStop(); } }
Windows ortamında çalışıyorsan, uygulamanın kamera erişim izni olması gerekir. Özellikle Windows 10/11'de gizlilik ayarlarını kontrol et.