EmguCV尝试读取或写入受保护的内存

时间:2012-04-02 22:00:06

标签: 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)
    {
        capture = new Capture();
        image = capture.QueryFrame();
        imageBox1.Image = image;
    }

我得到例外Attempted to read or write protected memory。我需要做些什么才能纠正这个问题?

2 个答案:

答案 0 :(得分:5)

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

我认为您的代码中存在错误。在应用程序生命周期内,您的Start方法将被多次调用(经常)。

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

只需将Capture实例移动到Form构造函数:

Capture capture;

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

答案 1 :(得分:0)

使用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;
    }