Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / GDI+

Updated AForge.NET DLLs to Accommodate .NET 4.6.1

5.00/5 (4 votes)
21 May 2020CPOL2 min read 10.3K   413  
Updated AForge.NET Version 2.2.6
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.

C#
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    try
    {
        lock (this)
        {
            Bitmap bit = (Bitmap)eventArgs.Frame.Clone(); // get a copy of the BitMap 
                                                          // from the VideoCaptureDevice
            Image oldBit = this.display.Image;

            if (!this.isResolutionSet)
            {
                // this is run once to set the resolution for the VideoRecorder
                this.Width = bit.Width;
                this.Height = bit.Height;
                this.isResolutionSet = true;
            }

            this.display.Image = new Bitmap(bit);  //(Bitmap)bit.Clone(); // displays the 
                                                   //current frame on the main form

            if (this.MotionDetection && !this.motionDetected)
            {
                // if motion detection is enabled 
                // and there weren't any previous motion detected
                Bitmap bit2 = (Bitmap)bit.Clone(); // clone the bits from the current frame

                try
                {
                    if (md.ProcessFrame(bit2) > 0.001) // feed the bits to the MD 
                    {
                        if (this.calibrateAndResume > 3)
                        {
                            // if motion was detected in 3 subsequent frames
                            Thread th = new Thread(MotionReaction);
                            th.Start();            // start the motion reaction thread
                        }
                        else
                            this.calibrateAndResume++;
                    }
                }
                catch
                {
                    Console.WriteLine("ERROR in md.ProcessFrame");
                }
                finally 
                {
                    if(bit2 != null)
                    {
                        bit2.Dispose();
                        bit2 = null;
                    }
                }
            }
            if (IsRecording)
            {
                // if recording is enabled we enqueue the current frame 
                // to be encoded to a video file
                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. :)

Image 1

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

License

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