简体   繁体   English

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

[英]How to Make Camera ON / OFF with PictureBox

IDE : Visual Studio 2010 Express IDE:Visual Studio 2010 Express
Lib : Emgu CV 2.2 Level : Beginner 库:Emgu CV 2.2级别:初学者

I've make Camera ON when Clicking PictureBox and viceversa, but it giving error : 单击PictureBox时,我已打开Camera,反之亦然,但它给出了错误消息:

Object reference not set to an instance of an object

Here the Event Handler : 这里是事件处理程序:

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);
    }
}

Calling from : 致电:

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

how to set if else parameter likely, any advice? 如何设置其他可能的参数,有什么建议?

Create a class level Boolean variable, then toggle it in your PictureBox's click event. 创建一个类级别的布尔变量,然后在您的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);
        }
    }
}

I expect your error is not being thrown by the picturebox click event but the ProcessFrame() event. 我希望您的错误不是由Picturebox click事件引发,而是由ProcessFrame()事件引发。 It will have the habit of firing once after you have removed the Application.Idle -= ProcessFrame; 在删除Application.Idle -= ProcessFrame;之后,它将具有触发一次的习惯Application.Idle -= ProcessFrame; however there will be no image in the event argument to work with. 但是在event参数中没有可以使用的图像。 Instead use this code as your ProcessFrame() event: 而是使用以下代码作为您的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
    }
}

Cheers, 干杯,

Chris 克里斯

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

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