anhngml / aforge

Automatically exported from code.google.com/p/aforge
Other
0 stars 0 forks source link

ToManagedImage method throwing an exception #179

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm doing motion detection on a camera feed. To grab frames I have a function:

public UnmanagedImage lastFrame = null;

public Bitmap LastFrame {
            get
            {
                lock (this)
                {
                    if (lastFrame!=null)
                        return lastFrame.ToManagedImage();
                }
                return null;
            }
        }

private void video_NewFrame(object sender, NewFrameEventArgs e)
        {

                if (e.Frame != null)
                {

                    lock (this)
                    {
                        if (lastFrame != null)
                            lastFrame.Dispose();
                        lastFrame = UnmanagedImage.FromManagedImage(e.Frame);
                    }
}
}

It all works fine but if I run the camera at high resolution (1600 x 1200) 
it'll work for a while then throw an exception:

"Parameter is not valid"

at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
   at AForge.Imaging.UnmanagedImage.ToManagedImage()
...
   at AForge.Video.DirectShow.VideoCaptureDevice.OnNewFrame(Bitmap image)
   at AForge.Video.DirectShow.VideoCaptureDevice.Grabber.BufferCB(Double sampleTime, IntPtr buffer, Int32 bufferLen)

Using Aforge v2.1.4

Original issue reported on code.google.com by ad...@activeunlimited.com on 18 Dec 2010 at 2:32

GoogleCodeExporter commented 9 years ago
Got this error. However not sure how to fix it. Here is what MSDN says:
http://msdn.microsoft.com/en-us/library/3z132tat.aspx
However debugger shows that pixel format is specified correctly. So MS does not 
tell something as usual.

I've reproduced the issue by running a long loop which calls both 
FromManagedImage() and ToManagedImage() without deposing temp images. And 
finally the exception was raised. So maybe it is raised not only on bad pixel 
format, but also when system runs out of memory.

Did you check your application that it disposes all temporary images and its 
memory usage does not grow?

Original comment by andrew.k...@gmail.com on 21 Dec 2010 at 3:10

GoogleCodeExporter commented 9 years ago
Hey, yeah memory usage is stable. I have 12 gig of ram, app doesn't go over 
400mb.

Original comment by ad...@activeunlimited.com on 21 Dec 2010 at 11:38