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

转换后的测试代码生成错误(C到VB.Net),vb.net,Vb.net,我已将C代码转换为VB.net,并应用了所需的小调整 但当我测试代码时,它会在以下指令所在的位置生成错误: If Not capture.Cued Then capture.Filename = counter & ".wmv" 我不是DirectShow的专家,我需要这些代码来继续学习。谁能给我慷慨的帮助 我将感谢任何帮助。多谢各位 Imports System.Collections.Generic Imports System.ComponentModel Imports

我已将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
在StartStopCapture中,不检查捕获是否为空。在表单显示事件或计时器事件中输入预览方法时,捕获对象将初始化。 在任何地方,您都有代码检查捕获对象是否为Nothing并避免使用它,但在StartStopCapture中,如果更改按钮文本,则没有此检查来保护中的代码

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
此外,内联按钮点击事件中的代码似乎是错误的

 If capture IsNot Nothing Then
      capture.Dispose()
      capture.[Stop]()
      capture.PreviewWindow = Nothing
 End iF
不要使用disposed对象并将其设置为Nothing,否则您可能会再次欺骗代码,以为捕获对象是有效的

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

这可能是什么错误?将异常添加到Catch中并复制/粘贴您的错误。另一种情况是NullReferenceException。捕获实例什么都不是,但没有进行测试。同样,代码中的注释和问题中的描述指向不同的部分。你应该重命名变量捕获-在VB中,变量名与类名相同会让事情变得很尴尬。如果您使用了IDE,那么IDE甚至可能会指出代码中存在的问题。还有过滤器和定时器。在C语言中,变量名区分大小写;在VB中并非如此。感谢您的宝贵贡献,但我不明白的是,为什么您在StartorStopCapture中重复您的代码,而该代码片段只属于previewDeviceNo。请注意,DeviceNo变量仅用于预览DeviceNo。capture=New Capturefilters.VideoInputDevicesdeviceNo,filters.AudioInputDevices0请注意,DeviceNo变量仅在previewDeviceNo中使用。@OscarMeza对,它是从代码中复制粘贴的。我不能在这里测试。然而,似乎你可以使用全球设备号码而不是设备我道歉有两个原因:1不太了解这个聊天论坛。我的英语很差,我总是用西班牙语翻译。没必要道歉。这个网站被全世界的人使用,并不是每个人都像我一样是madre lingua。然而,编程语言足够简单,可以让每个人都表达自己的概念。我在同一个地方遇到同样的错误:对象引用未设置为对象的实例。capture.Filename=计数器&wmv