Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wrote code using Aforge to capture the Video from the camera and its working fine. My problem is when I just start the Camera in the preview mode only without recording, the camera (preview window) freezes after 15 or 20 seconds. Then I noticed in the Task Manager that the cause is the process vhost32-clr.exe which starts to consume the memory until reaches +- 1,600 MB then the camera freezes.

Here is the code:

Public Sub callCam()

       PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        fiF = New FilterInfoCollection(FilterCategory.VideoInputDevice)
        finalVideo = New VideoCaptureDevice(fiF(0).MonikerString)
        finalVideo.VideoResolution = finalVideo.VideoCapabilities(0)
        
        AddHandler finalVideo.NewFrame, New NewFrameEventHandler(AddressOf Scapture)
        finalVideo.Start()
       
    End Sub


Here is the Scapture Sub code:

Private Sub Scapture(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)


        If ButtonVIDEO.BackColor = Color.Black Then 'In case of Preview only (No recording)

            Try
                BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'Put the data in the bitmap
                PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'present it in the Picture Box

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


        Else ' In case of Recording
            Try
                BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'Put the data in the bitmap
                PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'present it in the Picture Box
                VFwriter.WriteVideoFrame(BMP) 'Save in Memory

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub


What I have tried:

Then I modified the code in Scapture Sub to dispose the image frame as shown in the below code. The memory leak became very less, but still there is a leak that will freeze the app after a while.

Public Sub Scapture(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)

       ' If ButtonVIDEO.BackColor = Color.Black Then 'In case of Preview only (No recording)

       If ButtonVIDEO.Tag = "Preview" Then

           Try
               Dim img2 = Me.PictureBox1.Image
               Me.PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'present it in the Picture Box

               If img2 IsNot Nothing Then
                   img2.Dispose()

               End If


               Dim TmpBMP = BMP

               BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'Put the data in the bitmap
               If TmpBMP IsNot Nothing Then
                   TmpBMP.Dispose()
                   TmpBMP = Nothing
               End If






           Catch ex As Exception
               MsgBox(ex.Message)
           End Try

       Else ' In case of Recording
           Try

               Dim img2 = Me.PictureBox1.Image
                Me.PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'present it in the Picture Box
               If img2 IsNot Nothing  Then
                   img2.Dispose()

               End If

               Dim TmpBMP = BMP

               BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'Put the data in the bitmap
               VFwriter.WriteVideoFrame(BMP)
               If TmpBMP IsNot Nothing Then
                   TmpBMP.Dispose()

               End If


           Catch ex As Exception
               MsgBox(ex.Message)
           End Try

       End If
   End Sub


I also noticed that the memory leak becomes less or maight disappear if i do the following or one of them:

- Make the Picutebox size smaller
- Select lower resolution (for example 640x360 instead of 1280x720).

But I need to get rid of the memory leak without having to do the above.

Any help will be appreciated .
Posted
Updated 11-Aug-21 5:37am

1 solution

Have you tried using the Aforge VideoSourcePlayer control instead of the picturebox control? I think the VideoSourcePlayer control is more suited to your needs.
 
Share this answer
 
Comments
Eyadox 25-Aug-20 18:14pm    
Many thanks Hank, it really helped. Problem solved. I didn't use VideoSourcePlayer before, may you advise what beneficial properties/methods that I should consider for it in order to enhance the work.

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