-
Jun 16th, 2019, 05:20 PM
#1
Thread Starter
Lively Member
[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
-
Jun 16th, 2019, 06:34 PM
#2
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.
-
Jun 16th, 2019, 06:39 PM
#3
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.
-
Jun 16th, 2019, 07:16 PM
#4
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:
Imports AForge.Video.DirectShow Public Class Form1 Private ReadOnly videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load If videoDevices.Count = 0 Then '... Else For Each videoDevice In videoDevices '... Next End If End Sub 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.
-
Jun 16th, 2019, 08:18 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|