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

athlon128 2012-09-16 06:32:33
先上代码吧,我在网上找了写资料然后封装了一个类

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;注释了之后,消耗的内存就不断的增加。
我看了下,问题应该是出在

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
...全文
721 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ching126 2014-10-28
  • 打赏
  • 举报
回复
路过,我之前是Windows API 现在改用AForge,感觉还好
pemwoo 2012-12-03
  • 打赏
  • 举报
回复
我想用directx, aforge(或opencv), c#将视频画面切成两半,画面大小各为原来的一半大,输出为两路视频,楼主怎么编这个代码呢?用哪个类,函数,方法能做?能帮下忙指点我一下吗?
athlon128 2012-09-17
  • 打赏
  • 举报
回复
加了GC.Collect();强制回收,就好了。谢谢楼上
athlon128 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

释放newFrame
[/Quote]
能详细的说下吗....麻烦你了!
只在此山中 2012-09-17
  • 打赏
  • 举报
回复
释放newFrame

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧