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 .