日期:2014-05-17  浏览次数:20874 次

调用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 newFra