首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

调用AForge库开发摄像头时发生的内存有关问题

2012-09-22 
调用AForge库开发摄像头时发生的内存问题先上代码吧,我在网上找了写资料然后封装了一个类C# codeusing Sys

调用AForge库开发摄像头时发生的内存问题
先上代码吧,我在网上找了写资料然后封装了一个类

C# code
using System;using System.Collections.Generic;using System.Text;using System.Drawing;using AForge.Video.DirectShow;namespace Camera{    public delegate void NewFrameEventHandler(object sender, EventArgs e);    public class WebCamera    {        public event  NewFrameEventHandler NewFrameEvent;        private FilterInfoCollection videoDevices;        private VideoCaptureDevice videoSource = null;        public bool DeviceExist        { get; set; }        private Image newFrame;        public Image NewFrame        {            set            {                newFrame = value;            }            get            {                               return newFrame;                           }        }        public WebCamera()        {            DeviceExist = false;        }        public List<DeviceCapabilityInfo> GetDeviceCapability(DeviceInfo deviceInfo)        {            List<DeviceCapabilityInfo> deviceCapability = new List<DeviceCapabilityInfo>();            VideoCaptureDevice video = new VideoCaptureDevice(deviceInfo.MonikerString);            for (int i = 0; i < video.VideoCapabilities.Length; i++)            {                VideoCapabilities cap = video.VideoCapabilities[i];                DeviceCapabilityInfo capInfo = new DeviceCapabilityInfo(cap.FrameSize, cap.FrameRate);                deviceCapability.Add(capInfo);            }            return deviceCapability;        }        public List<DeviceInfo> GetCameras()        {            List<DeviceInfo> cameraList = new List<DeviceInfo>();            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);            int idx = 0;            foreach (FilterInfo device in videoDevices)            {                cameraList.Add(new DeviceInfo(device.Name, device.MonikerString, idx, FilterCategory.VideoInputDevice));                idx++;            }            return cameraList;        }        public FilterInfoCollection VideoDevices        {            get            {                return videoDevices;            }        }        public bool CloseVideo()        {            if (!(videoSource == null))                if (videoSource.IsRunning)                {                    videoSource.SignalToStop();                    videoSource.WaitForStop();                    DeviceExist = false;                }            videoSource = null;            return true;        }        public bool StartVideo(DeviceInfo device, DeviceCapabilityInfo info)        {            try            {                Size frameSize = info.FrameSize;                int rate = info.MaxFrameRate;                videoSource = new VideoCaptureDevice(device.MonikerString);                videoSource.DesiredFrameSize = frameSize;                videoSource.DesiredFrameRate = rate;                videoSource.NewFrame += new AForge.Video.NewFrameEventHandler(videoSource_NewFrame);                videoSource.Start();                DeviceExist = true;                return true;            }            catch            {                return false;            }        }        void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)        {            newFrame = (Image)eventArgs.Frame.Clone();                       if (NewFrameEvent != null)            {                NewFrameEvent(this, new EventArgs());            }        }          }    public class NewFrameEventArgs : EventArgs    {        public NewFrameEventArgs(Bitmap bitmap)        {            newFrame = bitmap;        }        Bitmap newFrame;        public Bitmap NewFrame        {            get            {                return newFrame;            }        }    }   //设备信息   public class DeviceInfo   {       public string Name;       public string MonikerString;       public int Index;       Guid Category;       public DeviceInfo(string name, string monikerString, int index) :           this(name, monikerString, index, Guid.Empty)       {       }       public DeviceInfo(string name, string monikerString, int index, Guid category)       {           Name = name;           MonikerString = monikerString;           Index = index;           Category = category;       }       public override string ToString()       {           return Name;       }   }   //设备能力   public class DeviceCapabilityInfo   {       public Size FrameSize;       public int MaxFrameRate;       public DeviceCapabilityInfo(Size frameSize, int maxFrameRate)       {           FrameSize = frameSize;           MaxFrameRate = maxFrameRate;       }       public override string ToString()       {           return string.Format("{0}x{1}  {2}fps", FrameSize.Width, FrameSize.Height, MaxFrameRate);       }   } 


前台调用是当这个类触发了NewFrameEvent事件之后,把NewFrame赋值给winform里的picturebox.Image
如果前台一直刷新picture.Image那么内存没什么异常,
当我把 // pictureBox1.Image = camera.NewFrame;注释了之后,消耗的内存就不断的增加。
我看了下,问题应该是出在
C# code
 void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)        {            newFrame = (Image)eventArgs.Frame.Clone();                       if (NewFrameEvent != null)            {                NewFrameEvent(this, new EventArgs());            }        }

里面的第一句话,如果注释掉,则内存不会一直增加,当然NewFrame也不会刷新了。
我把源码上传一下,麻烦高手帮我看看这个问题。谢谢
下载地址http://download.csdn.net/detail/athlon128/4573914

[解决办法]
释放newFrame

热点排行