如何使用PictureBox打开/关闭相机

时间:2014-03-13 01:07:24

标签: c# algorithm emgucv

IDE:Visual Studio 2010 Express
Lib:Emgu CV 2.2 等级:初学者

单击PictureBox时我将相机设为ON,反之亦然,但它给出了错误:

Object reference not set to an instance of an object

这里是事件处理程序:

private void pictureBoxCapture_Click(object sender, EventArgs e)
{
    try
    {
        if (Clicked == true) //i dont know how to make it right
        {
            Application.Idle -= ProcessFrame;
        }
        else
        {
            Application.Idle += ProcessFrame;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

来自:

private void ProcessFrame(object sender, EventArgs e)
{
    //Cap = new Emgu.CV.Capture();
    ImageFrame = Cap.QueryFrame();
    pictureBoxCapture.Image = ImageFrame.ToBitmap();
}

如何设置if else参数,有什么建议吗?

2 个答案:

答案 0 :(得分:2)

创建一个类级布尔变量,然后在PictureBox的click事件中切换它。

public partial class Form1 : Form
{
    bool Clicked; //Create this Class level variable to be used in your handler
    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBoxCapture_Click(object sender, EventArgs e)
    {
        Clicked =! Clicked; //Toggle your Boolean here
        try
        {
            if (Clicked)
            {
                Application.Idle -= ProcessFrame;
                FaceDetect();
            }
            else
            {
                Application.Idle += ProcessFrame;
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }
}

答案 1 :(得分:1)

我希望您的错误不会被图片框点击事件引发,而是ProcessFrame()事件。在删除Application.Idle -= ProcessFrame;之后,它会有一次触发的习惯,但是在事件参数中没有图像可以使用。而是将此代码用作ProcessFrame()事件:

private void ProcessFrame(object sender, EventArgs e)
{
    //Cap = new Emgu.CV.Capture();
    ImageFrame = Cap.QueryFrame();
    //Look for image content if null do nothing
    if(ImageFrame != null)
    {
        pictureBoxCapture.Image = ImageFrame.ToBitmap();
        //do any other operations on the image
    }
}

干杯,

克里斯