0

First a little background information: The purpose of this application is to capture images and save them automatically to a network directory that will be either created or appended using the input of the text box. This code DOES work on my computer (windows 7 home 64 bit). I've created it using microsoft visual basic express 2010.

However..... when attempting to run the application on a windows 10 tablet, I get the follow errors:

On form load:

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

Object reference not set to an instance of an object.

On button2_Click Event:

Object reference not set to an instance of an object.

Below is the entirety of the code.

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

As you can see toward the end of Form2.vb, I've wrapped imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg") in a Try-Catch block because I suspect it's some sort of problems with the pictureBox objects. The try catch block does indeed catch the exception, but I still have no idea what the problem is, why it happens on the tablet and not the PC, or how to fix it.

I've found similar questions, but none with a solution I can make use of.

user3479671
  • 424
  • 5
  • 22
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Andy Korneyev Feb 17 '16 at 14:34
  • @Andy Komeyev -I read that one, but I can't understand the answer or how it applies to my code. I'm pretty new BTW. – user3479671 Feb 17 '16 at 14:37
  • Sorry but this is way too much code. Read [this](http://stackoverflow.com/help/mcve), narrow the problem and adjust your question regarding a Minimal, Complete, and Verifiable example. Also, with the link @AndyKorneyev provided you should at least know that you have to specify the **exact** LOC which produces the Null Reference. – Alex B. Feb 17 '16 at 14:42
  • @Alex B - No problem, I've trimmed out the code I'm certain is't problematic.... I've just been burned before for posting snippets. – user3479671 Feb 17 '16 at 14:47
  • @Alex B - I'm not getting a null reference either. I noted the two errors I'm getting, the first on form load (which is 3 lines long), and the other I mentioned at the end of Button2_Click which I wraped in a Try-Catch block. – user3479671 Feb 17 '16 at 14:59
  • 1
    The `Object reference not set to an instance of an object` message is of type `NullReferenceException`. [MSDN NullReferenceException](https://msdn.microsoft.com/en-us/library/system.nullreferenceexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). Your line `webcam = New WebCam()` is not returning a value to the variable `webcam` which is likely a Win10 issue. Or your webcam on that tablet is not configured correctly. If you put a breakpoint on line `webcam.InitializeWebCam(imgVideo)` and then hover over the `webcam` variable you will see it has a value of `null`. – jaredbaszler Feb 17 '16 at 15:18
  • thanks for that. As I mentioned, the error only occurs on the tablet, making breakpoint debugging difficult (unless I installl vb express on it) – user3479671 Feb 17 '16 at 15:24
  • Good point on the dubugging on the tablet. Not sure what the type `WebCam()` is or what library it is in but I would research that to make sure it is Win10 compatible. – jaredbaszler Feb 17 '16 at 15:37
  • I'm using WebCam_Capture.dll (found here https://easywebcam.codeplex.com/downloads/get/96709). The example application throws the same errors. Guess I'm looking for a new library. Any suggestions? – user3479671 Feb 17 '16 at 16:38
  • I'd add error trapping and handling code to write out the stack trace to a log file to your app so you can know what line of code is erroring – Jeremy Feb 17 '16 at 17:55
  • After extensive research, I've found I'm not the only one to have this problem with this API. No one has a solution. The problem stems from the presence of more than one camera, and I've yet to find a way to specify which one to use. – user3479671 Feb 17 '16 at 18:05
  • A quick google search and there are some native Windows APIs you can hook into. It will take some overhauling of your code but you should be safe with these going forward. [EXAMPLE1](http://www.c-sharpcorner.com/UploadFile/2659af/capturing-and-saving-image-in-windows-10-application). This seems more indepth: [EXAMPLE2](http://www.codeproject.com/Articles/1021138/Building-camera-app-with-library-in-Windows) – jaredbaszler Feb 17 '16 at 18:06
  • Yes, tablets will have front and rear camera. I'd say you will need to change libraries or disable one of the two cameras on the tablet. The latter will obviously be much less work but not sure if that is an option with your app setup. – jaredbaszler Feb 17 '16 at 18:07
  • Device manager -> Imaging Devices -> right click the camera you don't want and disable. – jaredbaszler Feb 17 '16 at 18:10
  • 1
    I would just use a new library. The library link you referenced above has a very poor rating (2.5/5 stars), it is 6 years old, 3rd party and unsupported anymore. That is a better approach than expecting users to disable a camera. – jaredbaszler Feb 17 '16 at 18:20
  • Ok, I'm looking into the ones you showed me, but I can't find download links for the dll's, and the examples are all in C# – user3479671 Feb 17 '16 at 18:31
  • [Try this](https://code.msdn.microsoft.com/windowsapps/CameraCaptureUI-Sample-845a53ac/view/SourceCode) - looks pretty similar to the previous examples I posted and has a VB.Net example. It is directed at windows 8 but it is using the same libraries as the others. DLLs should be native and pre-loaded into Visual Studio. You may just have to add them to your project. – jaredbaszler Feb 17 '16 at 19:24
  • I'll let you know how it goes once I get a copy of VS that can open it. too new for VB 2010 express – user3479671 Feb 17 '16 at 19:45
  • You can download the latest version of Visual Studio Express [here](https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx). And it's free. – jaredbaszler Feb 17 '16 at 21:04
  • Thanks loads. I'm going to install that, but I need to update windows first. Meanwhile, I've been playing with the DirectX.Capture library, and I've conducted some tests which look promising – user3479671 Feb 18 '16 at 14:37

1 Answers1

1

Since you are using a library, EasyWebCam, that is outdated and not compatible with Win10, I would suggest switching libraries. Other options out there:

DirectX.Capture
Windows.Media.Capture

jaredbaszler
  • 2,579
  • 2
  • 23
  • 32
  • 1
    Accepted and upvoted! Thank you for your support on this. I'm now using the DirectX.Capture library sucessfully. – user3479671 Feb 18 '16 at 20:22