Click here to Skip to main content
15,916,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am an old and infrequent Basic guy. I took a 1 semester course in Basic in college in 1970, moved to Quick Basic and then Visual Basic in 1992. I do this as a hobby when some idea strikes my fancy.
I am trying to program a Laser Shooting Range that would capture a short laser "shot" and draw a hole on the "target" bitmap. Detecting it with a webcam video stream seems like a straightforward approach. Using Visual Basic Express 2010, I have finally taught myself how to capture the video from a USB camera and display it in an Aforge VideoSourcePlayer box. However, despite all the claims that it is trivial to do so, I cannot extract a bitmap frame from this stream to use the filters. I've read the documentation, and reviewed the samples, translated C# code to VB.NET, etc. over the last week, but I cannot get the program to recognize and capture a bitmap frame of the video. I'll include the operative code below, I just need the lines which get me the series of bitmap frames from this video so I can work with blob identification.

Form has a VideoSourcePlayer box and a PictureBox:
'create first video source
Dim cam As New VideoCaptureDevice(videoDevices(cboCameras.SelectedIndex) .MonikerString)
cam.DesiredFrameRate = 10
fMain.VideoSourcePlayer1.VideoSource = cam
'when each frame is received, f(cam_newFrame) is called
'cam.NewFrame += New NewFrameEventHandler(AddressOf cam_NewFrame) - This just gives me errors
'Dim img As Bitmap = DirectCast(cam.NewFrame.Clone(), Bitmap) - Displays a 'Blank' picture
Dim img As Bitmap = Bitmap.FromFile("C:\tgtMan-with holes.bmp") - This works, but it's not the video
fMain.PictureBox1.Image = img
Updated 29-Mar-23 4:28am


I did not have a chance to use this aforge videosurceplayer box that you are talking about, however, if as you say, when a frame is received a function cam_newFrame is called, this function should have an object passed to it as a parameter. This object should be the image you are looking for and then you should be able to store it as an image and then do your filters. if you could give the code for this function maybe i'll be able to help you out more.
Share this answer
Thank you for responding.
I cannot seem to "capture" the NewFrame Event. I've tried:

"Function cam_newFrame(ByVal sender As Object, ByVal eventArgs As AForge.Video.NewFrameEventArgs) Handles cam.NewFrame"
From this I get a warning and my function code (a simple TextBox entry for testing) does not execute:
"Warning - Function 'cam_newFrame' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."

I've tried the Sub below and again get nothing in my PictureBox
"Private Sub cam_NewFrame(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)
PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)

I appreciate any guidance.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900