Ok, I've been trying to do something specific with video feed from a webcam. I have a Lumenera Infinity 2 microscope that I am trying to pull feed from, and want to be able to modify the feed as it comes in. Since I couldn't find a way to do that using Video Source Player, I decided to instead pull each frame (max of 15fps for the camera) as a bitmap so I can do my modifications there.
The problem is: I have a HUGE memory leak. When I run the video just using the videoSourcePlayer, it hovers at using around 30 megs. When I run pulling the frames as bitmaps, it breaks 1 gig of memory in a matter of seconds.
What am I missing, here? I figured auto-garbage collection would scoop up the old frames as they became inaccessible. Should I try to force garbage collection on bitmap? Or is it something else entirely and I am noobishly missing it.
FilterInfoCollection captureDevices;
VideoCaptureDevice cam;
Bitmap bitmap;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
captureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (captureDevices.Count == 0)
throw new ApplicationException();
CameraSelectComboBox.Items.Clear();
foreach (FilterInfo device in captureDevices)
{
CameraSelectComboBox.Items.Add(device.Name);
}
CameraSelectComboBox.SelectedIndex = 0;
CameraSelectComboBox.Enabled = true;
}
catch (ApplicationException)
{
CameraSelectComboBox.Enabled = false;
}
}
private void connectButton_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(captureDevices[CameraSelectComboBox.SelectedIndex].MonikerString);
cam.NewFrame -= Handle_New_Frame; //Just to avoid the possibility of a second event handler being put on
cam.NewFrame += new AForge.Video.NewFrameEventHandler(Handle_New_Frame);
videoSourcePlayer1.Visible = false;
cam.Start();
//videoPictureBox1.Visible = false;
//videoSourcePlayer1.VideoSource = new VideoCaptureDevice(captureDevices[CameraSelectComboBox.SelectedIndex].MonikerString);
//videoSourcePlayer1.Start();
}
private void Handle_New_Frame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone();
videoPictureBox1.Image = bitmap;
}
Bitmap
isIDisposable
– Wasting