简体   繁体   English

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

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

I want to make an application which activates the webcam and detects movement. 我想制作一个激活网络摄像头并检测运动的应用程序。 So far I can turn ON and OFF the application. 到目前为止,我可以打开和关闭该应用程序。 Now I'm trying to implement motion detector. 现在,我正在尝试实现运动检测器。 The problem is - 'bitmap' does not exist in the current context. 问题是- “位图”在当前上下文中不存在。 The method from which it originates is private. 它起源的方法是私有的。 I tried with public, but still. 我曾与公众尝试过,但仍然如此。 And somehow Form1 is able to use it. 而且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();

        }
    }

}

Delete this code: 删除此代码:

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

And change the event code to this: 并将事件代码更改为此:

// 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
    }
}

It looks like you reused some code but put it in the wrong place. 看来您重用了一些代码,但将其放在错误的位置。

Don't take me wrong but putting a while loop inside button1_Click is an indication that you are just starting with c#. 不要误会我的意思,但是在button1_Click内放置一个while循环表明您刚刚开始使用c#。 Aforge.net is quite advanced stuff, I recommend you keep your programs simple as much as possible for now - motion detector may be too big of a chunk. Aforge.net是相当高级的东西,我建议您暂时使程序尽可能简单-运动检测器可能太大了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM