AccessViolationException 未处理 [VB.Net] [Emgucv]

AccessViolationException was unhandled [VB.Net] [Emgucv]

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这是我将图像设置到我的 PictureBox 后的错误。它工作正常,但稍后会弹出错误。

这是我的代码。

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Try
        Dim cap As New Capture() 'first line

        PictureBox1.Image = cap.QueryFrame.ToBitmap 'this line AccessViolationException
    Catch ex As Exception
        Timer1.Stop()
        MsgBox("CAMERA ERROR " & ex.Message)
    End Try
End Sub

Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
        Try
            Dim cap As New Capture() 'first line
            Select Case MetroTile1.Text
                Case "Capture"
                    Timer1.Start()
                    MetroTile1.Text = "OK"
                Case "OK"
                    Timer1.Stop()
                    frmStudentAddEdit.picImage.Image = PictureBox1.Image
                    MetroTile1.Text = "Capture"
                    Me.Close()
            End Select
        Catch ex As Exception
            Timer1.Stop()
        End Try
    End Sub

cap.QueryFrame.ToBitmapAccessViolationException 未处理 错误。

我该如何解决这个问题?是什么导致了这个错误?请帮助。

目标如下。

  1. Capture是表单的成员(不是每次都新建)
  2. oldImage 替换后被丢弃

Private mCapture As Capture

Private Sub Form12_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    mCapture = New Capture()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Try
        Dim oldImage = PictureBox1.Image
        Dim newFrame = mCapture.QueryFrame.ToBitmap
        PictureBox1.Image = newFrame.ToBitmap
        If oldImage IsNot Nothing Then oldImage.Dispose()
    Catch ex As Exception
        Timer1.Stop()
        MsgBox("CAMERA ERROR " & ex.Message)
    End Try
End Sub

Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
    Try
        Select Case MetroTile1.Text
            Case "Capture"
                Timer1.Start()
                MetroTile1.Text = "OK"
            Case "OK"
                Timer1.Stop()
                frmStudentAddEdit.picImage.Image = PictureBox1.Image
                MetroTile1.Text = "Capture"
                Me.Close()
        End Select
    Catch ex As Exception
        Timer1.Stop()
    End Try
End Sub