您的位置:首页 > 编程语言 > C#

C#使用aforge框架打开摄像头(续)

2015-10-23 18:01 731 查看
            昨天做了个小测试,把我之前写的用aforge框架打开摄像头的小程序,运行了一晚上,结果今天早上起来发现,程序挂了。调试了好几次,也没找到原因。

--------------------------问题

            刚刚 又做了测试,同时打开任务管理器,发现,该程序运行时间稍长,内存就开始紧张了。       如图:



       我今天特意做了记录,当程序运行了3.5小时左右,程序就炸了,无响应,查看此时其所占内存高达2G多。这下就苦恼了。

--------------------------解决方案

      东查西找,群里问,谷歌搜,终于在群里问到了解决方案。

      在摄像头捕获到每一帧图像后,做好系统资源回收。

--------------------------关键代码

          这里的代码,和我之前的笔记中的是一样的。下面是关键代码:

///------自定义函数,捕获每一帧图像并显示
private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs)
{
catchBtn.Visible = true;
//pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
//bm = (Bitmap)eventArgs.Frame.Clone();
bm = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bm;
///-----!!!!!!!!!!!!!! 注意,解决我的内存问题的代码就是下面的这一行
///---回收资源
GC.Collect();
///---throw new NotImplementedException();
}      就是这句
GC.Collect();      苦恼了我一天时间呢。

  但是还是解决啦 。 




    正在测试其能最长运行时间。

--------------------------完整源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
///---添加名称空间
using AForge.Video.DirectShow;
using AForge.Video;
using AForge;
using System.Threading;

namespace OS_webcam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///---声明变量
public FilterInfoCollection USE_Webcams = null;
public VideoCaptureDevice cam = null;
public Bitmap bm = null;

//---按钮被单击事件
private void startBtn_Click(object sender, EventArgs e)
{
try
{
Thread myThread = new Thread(MyThreadFunc);
myThread.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void MyThreadFunc()
{
if (startBtn.Text == "开始")
{
///--
startBtn.Text = "停止";
///---启动摄像头
cam.Start();
}
else
{
///--设置按钮提示字样
startBtn.Text = "开始";
///--停止摄像头捕获图像
cam.Stop();
}
}
//--窗口加载事件
private void Form1_Load(object sender, EventArgs e)
{
try
{
catchBtn.Visible = false;
///---实例化对象
USE_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
///---摄像头数量大于0
if (USE_Webcams.Count > 0)
{
///---禁用按钮
startBtn.Enabled = true;
///---实例化对象
cam = new VideoCaptureDevice(USE_Webcams[0].MonikerString);
///--关键
cam.VideoResolution = cam.VideoCapabilities[0];
///---绑定事件
cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
}
else
{
///--没有摄像头
startBtn.Enabled = false;
///---提示没有摄像头
MessageBox.Show("没有摄像头外设");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///------自定义函数,捕获每一帧图像并显示
private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs)
{
catchBtn.Visible = true;
//pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
//bm = (Bitmap)eventArgs.Frame.Clone();
bm = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bm;
///-----!!!!!!!!!!!!!! 注意,解决我的内存问题的代码就是下面的这一行
///---回收资源
GC.Collect();
///---throw new NotImplementedException();
}

///---窗口关闭事件
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
if (cam != null)
{
///---关闭摄像头
if (cam.IsRunning)
{
cam.Stop();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void catchBtn_Click(object sender, EventArgs e)
{
try
{
if (cam.IsRunning)
{
///---设置图像的名称和格式。
string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".png";
bm.Save(filepath);
///---保存成功
MessageBox.Show("捕捉图像保存成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void descriBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("版本:1.0\n\n作者:ROOTKIT^_^");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息