PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Aforge problem-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Aforge problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2013
    Posts
    106

    Resolved [RESOLVED] Aforge problem

    I found multiple examples of how to capture an image from a USB camera using Aforge. Although all these example start out the same way, they all get the same errors.

    In the particular example below, the line "If VideoDevices.Count = 0 Then", the error says: Count is not a member of FilterInfoCollection
    Even worse, on the line "For Each MySingleDevice In VideoDevices", the error says "Expression is of type "FilterInfoCollection", which is not a collection

    FilterInfoCollection is not a collection???

    I see this same problem in multiple examples.

    What is going on here?

    Code:
    Imports AForge.Video
    Imports AForge.Video.DirectShow
    
    Public Class Form1
        Dim VideoCaptureSource As VideoCaptureDevice
        Dim VideoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim MySingleDevice As FilterInfo
    
            If VideoDevices.Count = 0 Then
                ComboBox.Items.Add("No Video Devices")
            Else
                For Each MySingleDevice In VideoDevices
                    ComboBox.Items.Add(MySingleDevice.Name)
                Next
            End If
    
            ComboBox.SelectedIndex = 0
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            VideoSourcePlayer.SignalToStop()
            VideoSourcePlayer.WaitForStop()
            VideoDevices = Nothing
            VideoCaptureSource = Nothing
        End Sub
    
        Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
            If ComboBox.SelectedItem <> "No Video Devices" Then
                If VideoSourcePlayer.IsRunning = True Then
                    VideoSourcePlayer.SignalToStop()
                    VideoSourcePlayer.WaitForStop()
                End If
    
                VideoCaptureSource = New VideoCaptureDevice(VideoDevices(ComboBox.SelectedIndex).MonikerString)
                VideoSourcePlayer.VideoSource = VideoCaptureSource
                VideoSourcePlayer.Start()
            End If
        End Sub
    
        Private Sub Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Capture.Click
            Try
                Dim strFilename As String
                strFilename = "Image-" & Format(Now, "yyyy-MMM-dd, H-mm-ss-fff") & ".jpg"
    
                If VideoSourcePlayer.IsRunning = True Then
                    VideoSourcePlayer.GetCurrentVideoFrame.Save(System.Windows.Forms.Application.StartupPath & "\" & strFilename, System.Drawing.Imaging.ImageFormat.Jpeg)
                    Label.Text = "Image Saved : " & strFilename
                End If
            Catch ex As Exception
                MessageBox.Show("Try taking snapshot again when video image is visible.", "Cannot Save Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    
        Private Sub Quit_Click(sender As Object, e As EventArgs) Handles Quit.Click
            Application.Exit()
        End Sub
    End Class

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,585

    Re: Aforge problem

    I tried to view the documentation for that class on the web but the page wouldn't load. Perhaps use the Object Explorer in VS to take a look at the class declaration and see what it actually is. The usage seems to suggest that it implements the IEnumerable interface at the very least, but your error messages seem to suggest not. The Object Explorer will show you what members it does have, as will Intellisense.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,585

    Re: Aforge problem

    OK, here's the actual source code for that class:

    https://csharp.hotexamples.com/site/...rsal.AForgeNET

    That says that it inherits CollectionBase, which implements IList, which extends IEnumerable. When you get that error message when trying to use an object as the source for a For Each loop, it's telling you that it doesn't implement IEnumerable. That seems rather bizarre. I have no explanation for that.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,585

    Re: Aforge problem

    OK, I just created a new VB WinForms project targeting .NET 4.7.2 and added version 2.2.5 of the NuGet package for AForge.Video.DirectShow. I was then able to write this code:
    vb.net Code:
    1. Imports AForge.Video.DirectShow
    2.  
    3. Public Class Form1
    4.  
    5.     Private ReadOnly videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    8.         If videoDevices.Count = 0 Then
    9.             '...
    10.         Else
    11.             For Each videoDevice In videoDevices
    12.                 '...
    13.             Next
    14.         End If
    15.     End Sub
    16.  
    17. End Class
    and it compiled without issue. There may be something broken in your project so I suggest that you try with a new test project and see whether it fails again.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2013
    Posts
    106

    Re: Aforge problem

    [QUOTE=There may be something broken in your project so I suggest that you try with a new test project and see whether it fails again.[/QUOTE]

    I saved the code and pasted it into a new project as you suggested and it works. Something in the project must have been broken.

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width