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

时间:2016-08-19 11:12:11

标签: vb.net emgucv

  

尝试读取或写入受保护的内存。这通常是一个   表明其他内存已损坏。

将Image设置为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.ToBitmap AccessViolationException未处理错误。

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

1 个答案:

答案 0 :(得分:0)

瞄准以下内容。

  1. Capture是表单的成员(每次都不是新创建的)
  2. oldImage在替换
  3. 后被处理掉
    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