C# Camera Capture with AForge

In order to utilize AForge for capturing camera information, you must first install the AForge.NET Framework. Then, you can follow these steps to implement camera information capture using C# code.

  1. Use the AForge library:
using AForge.Video;
using AForge.Video.DirectShow;
  1. Initialize the camera device.
private VideoCaptureDevice videoSource;

private void InitializeCamera()
{
    FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
    videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
    videoSource.Start();
}
  1. Implement video frame processing event.
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap videoFrame = (Bitmap)eventArgs.Frame.Clone();
    // 在这里对视频帧进行处理,例如显示到窗口上
    pictureBox1.Image = videoFrame;
}
  1. Start capturing video stream.
private void StartCapture()
{
    videoSource.Start();
}
  1. Stop capturing video feed.
private void StopCapture()
{
    if (videoSource.IsRunning)
    {
        videoSource.SignalToStop();
        videoSource.WaitForStop();
    }
}

By following the steps above, you can use AForge to capture camera information and process video frames. In the event method for processing video frames, you can perform various operations on the frames as needed, such as saving or displaying them.

bannerAds