C# EmguCV试图读取或写入受保护的内存

C# EmguCV试图读取或写入受保护的内存,c#,exception,exception-handling,emgucv,C#,Exception,Exception Handling,Emgucv,我有以下代码使用EmgucV在imagebox中显示图像: Capture capture; Image<Bgr, Byte> image; public Form1() { InitializeComponent(); Application.Idle += new EventHandler(Start); } void Start(object sender, EventArgs e) {

我有以下代码使用EmgucV在imagebox中显示图像:

    Capture capture;
    Image<Bgr, Byte> image;

    public Form1()
    {
        InitializeComponent();
        Application.Idle += new EventHandler(Start);
    }
    void Start(object sender, EventArgs e)
    {
        capture = new Capture();
        image = capture.QueryFrame();
        imageBox1.Image = image;
    }
捕获;
图像;
公共表格1()
{
初始化组件();
Application.Idle+=新的EventHandler(开始);
}
无效开始(对象发送方,事件参数e)
{
捕获=新捕获();
image=capture.QueryFrame();
imageBox1.Image=Image;
}

我尝试读取或写入受保护内存时出现异常
。我需要做什么来更正此问题?

这表明可能存在本机内存泄漏

我认为你的代码有错误。在应用程序生命周期中,
Start
方法将被多次调用(非常频繁)

看起来您应该在应用程序中只使用一个捕获对象

只需将捕获实例化移动到表单构造函数:

Capture capture;

public Form1()
{
    InitializeComponent();
    Application.Idle += new EventHandler(Capture);
    capture = new Capture();
}
void Capture(object sender, EventArgs e)
{
    imageBox1.Image = capture.QueryFrame(); 
}

使用Windows Form c#的Emgu CV版本3.4.1的当前修复程序;添加一个按钮称之为btnCapture,添加一个PictureBox控件出于此回答的目的,保留其默认名称。希望这个代码示例有帮助

    public VideoCapture capture;
    private void btnCatpure_Click(object sender, EventArgs e)
    {
        // each click a single frame will be capture and then display in the control.
        Mat iframe = new Mat();
        capture.Retrieve(iframe, 0);
        Mat grayFrame = new Mat();
        CvInvoke.CvtColor(iframe, grayFrame, ColorConversion.Bgr2Gray);

        pictureBox1.Image =  iframe.Bitmap;
        pictureBox1.Image = grayFrame.Bitmap;
    }