转换的测试代码生成错误(C#到VB.Net)

时间:2014-09-12 19:01:26

标签: vb.net

我已将C#代码转换为VB.net并已应用所需的小调整。

但是当我测试代码时,它会在这些指令的位置产生错误:

If Not capture.Cued Then
    capture.Filename = counter & ".wmv"

我不是DirectShow的专家,我需要该代码继续学习。请有人能给我慷慨的帮助吗?

我将不胜感激任何帮助。谢谢。

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports DirectX.Capture
Imports DShowNET

Public Class MyWebCam
    Inherits Form
    ' Muaz Khan (@muazkh) - http://muaz-khan.blogspot.com 


    Private capture As Capture = Nothing
    Private filters As Filters = Nothing

    Private counter As Integer = 1
    Private timer As New Timer()

    Private deviceNumber As Integer = 0

    Public Sub New()
        InitializeComponent()
    End Sub

    '============================================================================


    Private Sub Form1_Shown(sender As Object, e As EventArgs)
        filters = New Filters()

        If filters.VideoInputDevices IsNot Nothing Then
            Try
                preview(deviceNumber)
            Catch ex As Exception
                MessageBox.Show("Maybe any other software is already using your WebCam." & vbLf & vbLf & " Error Message: " & vbLf & vbLf & ex.Message)
            End Try
        Else
            btnStartVideoCapture.Enabled = False
            MessageBox.Show("No video device connected to your PC!")
        End If

        timer.Interval = 600000
        ' 10 minutes!
        AddHandler timer.Tick, Function(obj, evt)
                                   If btnStartVideoCapture.Text = "STOP" Then
                                       counter += 1

                                       If capture IsNot Nothing AndAlso counter > 1 Then
                                           capture.[Stop]()
                                           If Not capture.Cued Then
                                               capture.Filename = counter & ".wmv"
                                           End If
                                           capture.Cue()
                                           capture.Start()
                                       End If
                                   End If

                               End Function

        If filters.VideoInputDevices IsNot Nothing Then
            For i = 0 To filters.VideoInputDevices.Count - 1
                Dim device = filters.VideoInputDevices(i)

                Dim btn = New Button()

                btn.Text = i.ToString()
                btn.ForeColor = Color.White
                btn.BackColor = Color.DarkSlateBlue
                btn.Width = 25

                AddHandler btn.Click, Function(obj, evt)
                                          Dim thisButton = DirectCast(obj, Button)

                                          If Integer.Parse(thisButton.Text) <> deviceNumber Then
                                              If capture IsNot Nothing Then
                                                  capture.Dispose()
                                                  capture.[Stop]()
                                                  capture.PreviewWindow = Nothing
                                              End If

                                              deviceNumber = Integer.Parse(thisButton.Text)
                                              preview(deviceNumber)
                                          End If

                                      End Function

                FlowLayoutPanel1.Controls.Add(btn)
            Next
        End If
    End Sub

    '============================================================================


    Private Sub preview(deviceNo As Integer)
        Try

            ' MessageBox.Show("deviceNo = > " + deviceNo);
            capture = New Capture(filters.VideoInputDevices(deviceNo), filters.AudioInputDevices(0))

            capture.PreviewWindow = Panel1

            If btnStartVideoCapture.Text = "STOP" Then
                counter += 1
                If Not capture.Cued Then
                    capture.Filename = counter & ".wmv"
                End If
                capture.Cue()
            End If

            capture.Start()
        Catch
        End Try
    End Sub

    '============================================================================


    Private Sub btnStartVideoCapture_Click(sender As System.Object, e As System.EventArgs) Handles btnStartVideoCapture.Click
        startOrStopCapturing(capture)
    End Sub

    '============================================================================


    Private Sub startOrStopCapturing(capture As Capture)
        btnStartVideoCapture.Visible = False

    '**** THE ERROR BEGINS TO APPEAR HERE ************
    '****
    '****
        If capture IsNot Nothing Then
            capture.[Stop]()
        End If
        If timer.Enabled Then
            timer.[Stop]()
        End If

        If btnStartVideoCapture.Text = "START" Then
            btnStartVideoCapture.Text = "STOP"
            btnStartVideoCapture.BackColor = Color.Maroon

            Try
                If Not capture.Cued Then
                    capture.Filename = counter & ".wmv"
                End If

                capture.Cue()
                capture.Start()

                timer.Start()
            Catch ex As Exception
                MessageBox.Show("Error Message: " & vbLf & vbLf & ex.Message)
            End Try
        Else
            btnStartVideoCapture.Text = "START"
            btnStartVideoCapture.BackColor = Color.DarkSlateBlue
        End If
        btnStartVideoCapture.Visible = True
    End Sub

    '============================================================================

    Private Sub InitializeComponent()
        Me.btnStartVideoCapture = New System.Windows.Forms.Button()
        Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.SuspendLayout()
        '
        'btnStartVideoCapture
        '
        Me.btnStartVideoCapture.BackColor = System.Drawing.Color.DarkSlateBlue
        Me.btnStartVideoCapture.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnStartVideoCapture.ForeColor = System.Drawing.Color.White
        Me.btnStartVideoCapture.Location = New System.Drawing.Point(498, 421)
        Me.btnStartVideoCapture.Name = "btnStartVideoCapture"
        Me.btnStartVideoCapture.Size = New System.Drawing.Size(89, 38)
        Me.btnStartVideoCapture.TabIndex = 2
        Me.btnStartVideoCapture.Text = "START"
        Me.btnStartVideoCapture.UseVisualStyleBackColor = False
        '
        'FlowLayoutPanel1
        '
        Me.FlowLayoutPanel1.Location = New System.Drawing.Point(0, 421)
        Me.FlowLayoutPanel1.Name = "FlowLayoutPanel1"
        Me.FlowLayoutPanel1.Size = New System.Drawing.Size(502, 38)
        Me.FlowLayoutPanel1.TabIndex = 3
        '
        'Panel1
        '
        Me.Panel1.Location = New System.Drawing.Point(0, -3)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(587, 418)
        Me.Panel1.TabIndex = 4
        '
        'Form1
        '
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(585, 458)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.FlowLayoutPanel1)
        Me.Controls.Add(Me.btnStartVideoCapture)
        Me.Name = "Form1"
        Me.ResumeLayout(False)

    End Sub
    Private WithEvents btnStartVideoCapture As System.Windows.Forms.Button
    Friend WithEvents FlowLayoutPanel1 As System.Windows.Forms.FlowLayoutPanel
    Friend WithEvents Panel1 As System.Windows.Forms.Panel

End Class

1 个答案:

答案 0 :(得分:1)

startStopCapturing中,您无法检查capture IsNothing 。当您在preview形式事件或计时器事件中输入Shown方法时,将初始化捕获对象。
你到处都有代码来检查IsNothing的捕获对象并避免使用它,但在startStopCapturing中,你没有这个检查来保护改变按钮文本的IF中的代码。

Private Sub startOrStopCapturing(capture As Capture)
    btnStartVideoCapture.Visible = False
    If capture IsNot Nothing Then
        capture.[Stop]()
        capture.Dispose()
        capture = Nothing
        If timer.Enabled Then
            timer.[Stop]()
        End If
        btnStartVideoCapture.Text = "START"
        btnStartVideoCapture.BackColor = Color.DarkSlateBlue
    Else
        Try
           capture = New Capture(filters.VideoInputDevices(deviceNumber), _ 
                                 filters.AudioInputDevices(0))

            capture.Filename = counter & ".wmv"
            capture.Cue()
            capture.Start()
            timer.Start()
            btnStartVideoCapture.Text = "STOP"
            btnStartVideoCapture.BackColor = Color.Maroon

        Catch ex As Exception
            MessageBox.Show("Error Message: " & vbLf & vbLf & ex.Message)
        End Try
    End If
    btnStartVideoCapture.Visible = True
End Sub

此外,内联button_click事件中的代码似乎是错误的

 If capture IsNot Nothing Then
      capture.Dispose()
      capture.[Stop]()
      capture.PreviewWindow = Nothing
 End iF

不要使用已处置的对象并将其设置为Nothing,因为如果没有它,您可以再次欺骗您的代码以确保捕获对象有效

 If capture IsNot Nothing Then
      capture.[Stop]()
      capture.PreviewWindow = Nothing
      capture.Dispose()
      capture = Nothing
 End iF