好的,我一直在尝试对来自网络摄像头的视频源做一些特定的事情。我有一个 Lumenera Infinity 2 显微镜,我正试图从中提取饲料,并希望能够在饲料进入时对其进行修改。由于我找不到使用视频源播放器的方法,所以我决定改为将每一帧(相机最大 15fps)拉为位图,以便我可以在那里进行修改。
问题是:我有一个巨大的内存泄漏。当我只使用 videoSourcePlayer 运行视频时,它徘徊在使用大约 30 兆。当我将帧作为位图运行时,它会在几秒钟内破坏 1 gig 的内存。
我在这里错过了什么?我认为自动垃圾收集会在旧框架变得无法访问时将它们铲起。我应该尝试在位图上强制垃圾收集吗?或者它完全是别的东西,我很想念它。
FilterInfoCollection captureDevices;
VideoCaptureDevice cam;
Bitmap bitmap;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
captureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (captureDevices.Count == 0)
throw new ApplicationException();
CameraSelectComboBox.Items.Clear();
foreach (FilterInfo device in captureDevices)
{
CameraSelectComboBox.Items.Add(device.Name);
}
CameraSelectComboBox.SelectedIndex = 0;
CameraSelectComboBox.Enabled = true;
}
catch (ApplicationException)
{
CameraSelectComboBox.Enabled = false;
}
}
private void connectButton_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(captureDevices[CameraSelectComboBox.SelectedIndex].MonikerString);
cam.NewFrame -= Handle_New_Frame; //Just to avoid the possibility of a second event handler being put on
cam.NewFrame += new AForge.Video.NewFrameEventHandler(Handle_New_Frame);
videoSourcePlayer1.Visible = false;
cam.Start();
//videoPictureBox1.Visible = false;
//videoSourcePlayer1.VideoSource = new VideoCaptureDevice(captureDevices[CameraSelectComboBox.SelectedIndex].MonikerString);
//videoSourcePlayer1.Start();
}
private void Handle_New_Frame(object sender, NewFrameEventArgs eventArgs)
{
bitmap = (Bitmap)eventArgs.Frame.Clone();
videoPictureBox1.Image = bitmap;
}