Projects using AForge.NET DLLs can now update their projects .NET Target Framework to something greater than .NET 2.0
Introduction
During this virus lockdown, I've been working from home developing software. A few days a week, I head in to my office and noticed some things on my desk that were moved around... So I decided to build a motion detector and video capture application using the webcam I had thrown into my desk drawer to see who is in the office while I'm not there.
Searching
I did some searching and found a nice app: MicroDVR - Simple Video Security Using Webcams.
I downloaded the app and compiled it and it ran pretty well except it was crashing after a while... but because the project was developed with the Target Framework set to .NET 2.0 (or was it 3.5, I can't remember), I couldn't run the memory usage tool in my Visual Studio 2019... apps have to be developed with at least 4.0 in order to use that feature.
My Conundram
So, to accommodate the memory usage tool, I went into the projects properties and set the Target Framework to 4.6.1 and hit the project's Rebuild .... Alas, errors galore complaining that all the AForge DLLs referenced in the app were compiled with .NET 2.0 and would not allow me to up the framework!
My Solution
Well, I was able to find the AForge projects in GitHub at https://github.com/andrewkirillov/AForge.NET.
I was able to clone the projects and compile the DLLs. I recompiled every DLL with the Target Framework of 4.6.1 and I changed the AForge DLL versions to 2.2.6... Now, I'll admit I'm a complete rookie using GitHub so I wasn't quite sure how to submit the changes back to GitHub so I didn't bother with it at the moment.
I was now able to up the Framework to 4.6.1 and compile the MicroDVR
project and run it... Sure enough, it was consuming over a Gigs worth of RAM quite quickly... I found the memory leak fairly quickly and fixed it in the code posted here and posted in the comments section of that project's article.
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
lock (this)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
Image oldBit = this.display.Image;
if (!this.isResolutionSet)
{
this.Width = bit.Width;
this.Height = bit.Height;
this.isResolutionSet = true;
}
this.display.Image = new Bitmap(bit);
if (this.MotionDetection && !this.motionDetected)
{
Bitmap bit2 = (Bitmap)bit.Clone();
try
{
if (md.ProcessFrame(bit2) > 0.001)
{
if (this.calibrateAndResume > 3)
{
Thread th = new Thread(MotionReaction);
th.Start();
}
else
this.calibrateAndResume++;
}
}
catch
{
Console.WriteLine("ERROR in md.ProcessFrame");
}
finally
{
if(bit2 != null)
{
bit2.Dispose();
bit2 = null;
}
}
}
if (IsRecording)
{
using (Graphics gr = Graphics.FromImage(bit))
{
using (Pen p = new Pen(Color.Red))
{
p.Width = 5.0f;
using (Font myFont = new Font("Tahoma", 10, FontStyle.Bold))
{
gr.DrawString(DateTime.Now.ToString(),
myFont, Brushes.Red, new Point(2, 2));
}
}
}
frames.Enqueue((Bitmap)bit.Clone());
}
bit.Dispose();
if (oldBit != null)
{
oldBit.Dispose();
oldBit = null;
}
}
}
catch (InvalidOperationException ex)
{
string msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
Console.WriteLine($"EXCEPTION IN cam_NewFrame(): {msg}");
}
}
With my one webcam, it now only consumes around 84MB of RAM, much better now. :)
Anyways, I wanted to share the DLLs in this article for anyone using the AForge DLLs who want to update their project's .NET Framework.
Disclaimer
I changed no code in the AForge DLLs included in the attached ZIP file other than the versions and the compiled Target Framework. The latest official version from AForge was 2.2.5 so my version in this zip is 2.2.6. Keep a copy of the official version in case these don't work for you.
History
- 21st May 2020: Initial version