对象引用未设置为对象的实例 - vb.net

时间:2016-02-17 14:32:01

标签: vb.net winforms

首先是一些背景信息:此应用程序的目的是捕获图像并将其自动保存到网络目录中,该目录将使用文本框的输入创建或附加。这段代码可以在我的电脑上运行(Windows 7家64位)。我使用microsoft visual basic express 2010创建了它。

然而.....当试图在Windows 10平板电脑上运行应用程序时,我收到以下错误:

表单加载:

An error occurred while capturing the image.  The video capture will now be terminated.

Object reference not set to an instance of an object.

按钮2_点击事件:

Object reference not set to an instance of an object.

以下是整个代码。

Form2.vb

Public Class Form2
    Public scanIsSet As Boolean
    Private webcam As WebCam
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        webcam = New WebCam()
        webcam.InitializeWebCam(imgVideo)
        webcam.Start()
        scanIsSet = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim CFGfile As String
        Dim SaveDir As String
        Dim imgIndex As Integer
        Dim existingImages() As String
        SaveDir = "C:\somepath\"


            'save image to directory with index number
            Try
                imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg")
            Catch ex As Exception
                MsgBox("error while accessing object imgCapture" & ex.Message)
            End Try
            imgIndex = imgIndex + 1

        Else
            Beep()
            MsgBox("Please scan or type in order number first")
        End If
    End Sub
End Class

WebCam.vb

Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports WebCam_Capture
Imports System.Collections.Generic
Imports ZXing
Imports ZXing.OneD

'Design by Pongsakorn Poosankam
Class WebCam
    Public scanz As Boolean
    Public Sub setScan(ByVal x As Boolean)
        scanz = x
    End Sub
    Private webcam As WebCamCapture
    Private _FrameImage As System.Windows.Forms.PictureBox
    Private FrameNumber As Integer = 30
    Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Forms.PictureBox)
        webcam = New WebCamCapture()
        webcam.FrameNumber = CULng((0))
        webcam.TimeToCapture_milliseconds = FrameNumber
        AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured
        _FrameImage = ImageControl
    End Sub

    Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal e As WebcamEventArgs)
        _FrameImage.Image = e.WebCamImage
        If scanz = True Then
            Dim BCreader As New ZXing.BarcodeReader

            'BCreader.Options.TryHarder = True
            Try
                Dim resu As Result = BCreader.Decode(e.WebCamImage)

                Form2.OrderNumber.Text = resu.Text
                setScan(False)
                Form2.Label2.Text = ""
                Beep()
            Catch ex As Exception
                'do nothing
            End Try
        End If
    End Sub

    Public Sub Start()
        webcam.TimeToCapture_milliseconds = FrameNumber
        webcam.Start(0)
    End Sub

    Public Sub [Stop]()
        webcam.[Stop]()
    End Sub

    Public Sub [Continue]()
        ' change the capture time frame
        webcam.TimeToCapture_milliseconds = FrameNumber

        ' resume the video capture from the stop
        webcam.Start(Me.webcam.FrameNumber)
    End Sub

    Public Sub ResolutionSetting()
        webcam.Config()
    End Sub

    Public Sub AdvanceSetting()
        webcam.Config2()
    End Sub

End Class

正如您在 Form2.vb 结束时所看到的,我已将imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg")包装在Try-Catch块中,因为我怀疑它是pictureBox对象的某些问题。 try catch块确实捕获了异常,但我仍然不知道问题是什么,为什么它发生在平板电脑而不是PC上,或者如何解决它。

我发现了类似的问题,但没有找到我可以使用的解决方案。

2 个答案:

答案 0 :(得分:1)

由于你使用的是EasyWebCam,它过时且与Win10不兼容,我建议切换库。其他选择:

DirectX.Capture
Windows.Media.Capture

答案 1 :(得分:-1)

我找到了解决方案,但我不知道您现在是否需要它,问题是如果您更改了 PICTUREBOX 名称,则作为参考使用您已更改的确切名称。例如,如果我将所有 PICTUREBOX 名称更改为 -

PictureBox_A1 , PictureBox_A2 ,... 等等然后我的参考应该是 -

Dim r As DataRow

        For Each r In t1.Rows
            CType(Controls("PictureBox_" & r(2)), PictureBox).Image = bookedicon
        Next

我的参考是 - “PictureBox_”