AForge.NET - 名称在当前上下文中不存在

AForge.NET - name does not exist in the current context

我想制作一个可以激活网络摄像头并检测移动的应用程序。到目前为止,我可以打开和关闭应用程序。现在我正在尝试实施运动检测器。问题是 - 'bitmap' 在当前上下文中不存在。 它源自的方法是私有的。我尝试使用 public,但仍然如此。并且 Form1 能够以某种方式使用它。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Vision.Motion;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        // create motion detector
        MotionDetector detector = new MotionDetector(new SimpleBackgroundModelingDetector(), new MotionAreaHighlighting());

        public VideoCaptureDevice videoSource = null;
        public FilterInfoCollection videoDevices;


        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // enumerate video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            // create video source
            videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);

            // set NewFrame event handler
            videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

            // continuously feed video frames to motion detector
            while (true)
            {
                // process new video frame and check motion level
                if (detector.ProcessFrame(bitmap) > 0.02) << // PROBLEM
                {
                    // ring alarm or do something else
                }
            }

            // start the video source
            videoSource.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            videoSource.Stop();
            pictureBox1.Image = null;
        }

        // Method:
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // get new frame
            Bitmap bitmap = eventArgs.Frame;
            // process the frame

            // Put image in picture box
            pictureBox1.Image = (Bitmap)bitmap.Clone();

        }
    }

}

删除此代码:

while (true)
{
    // process new video frame and check motion level
    if (detector.ProcessFrame(bitmap) > 0.02) << // PROBLEM
    {
        // ring alarm or do something else
    }
}

并将事件代码更改为:

// Method:
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    // get new frame
    Bitmap bitmap = eventArgs.Frame;
    // process the frame

    // Put image in picture box
    pictureBox1.Image = (Bitmap)bitmap.Clone();

    if (detector.ProcessFrame(bitmap) > 0.02) << // PROBLEM
    {
        // ring alarm or do something else
    }
}

您似乎重复使用了一些代码,但将其放在了错误的位置。

不要误会我的意思,在 button1_Click 中放置一个 while 循环表明您刚刚开始使用 C#。 Aforge.net 是相当高级的东西,我建议您现在尽可能保持程序简单 - 运动检测器可能太大了。