IDE:Visual Studio 2010 Express
库:Emgu CV 2.2
级别:初学者
我在单击 PictureBox 时打开了相机,反之亦然,但它给出了错误:
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 参数,有什么建议吗?
最佳答案
创建一个类级别的 bool 变量,然后在 PictureBox 的点击事件中切换它。
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);
}
}
}
关于c# - 如何使用 PictureBox 打开/关闭相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367162/