C#,每5秒连续保存网络摄像头的帧
当运行基于GUI的代码时,它实际上会打开一个网络摄像机提要并每隔5秒保存一次帧,在随机运行时间(可能是5分钟或20分钟)后,出现“另一进程正在使用的对象”异常,可能与当前帧的保存有关。有什么想法是问题的原因,以及应该做哪些代码修改吗C#,每5秒连续保存网络摄像头的帧,c#,image,video,webcam,aforge,C#,Image,Video,Webcam,Aforge,当运行基于GUI的代码时,它实际上会打开一个网络摄像机提要并每隔5秒保存一次帧,在随机运行时间(可能是5分钟或20分钟)后,出现“另一进程正在使用的对象”异常,可能与当前帧的保存有关。有什么想法是问题的原因,以及应该做哪些代码修改吗 public partial class Form1 : Form { private FilterInfoCollection CaptureDevices; private VideoCaptureDevice video
public partial class Form1 : Form
{
private FilterInfoCollection CaptureDevices;
private VideoCaptureDevice videoSource;
bool blncapturing;
public Form1()
{
InitializeComponent();
timer1.Enabled = false;
timer1.Interval = 5000;
blncapturing = false;
}
private void button1_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
videoSource.Start();
timer1.Enabled = true;
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (pictureBox1.Image != null)
((IDisposable)pictureBox1.Image).Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void Form1_Load_1(object sender, EventArgs e)
{
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevices)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
}
private void button2_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox2.Image = null;
pictureBox2.Invalidate();
}
private void button3_Click(object sender, EventArgs e)
{
Capturing();
}
private void button4_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning == true)
{
videoSource.Stop();
}
Application.Exit(null);
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
Capturing();
timer1.Start();
}
private void Capturing()
{
try
{
if (blncapturing == false)
{
blncapturing = true;
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
string strGrabFileName = String.Format("C:\\Users\\echristo\\Desktop\\trial\\Snapshot_{0:yyyyMMdd_hhmmss.fff}.bmp", DateTime.Now);
pictureBox2.Image.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg) ;
blncapturing = false;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
如果从不同线程访问对象,则需要一个锁。
当您从不同的线程访问控件时,也使用invoke
private readonly object myLock = new object();
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (myLock) {
this.Invoke((Action)(() => {
if (pictureBox1.Image != null)
((IDisposable)pictureBox1.Image).Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
// you can try with timer1 inside the lock
lock (myLock) {
// you don't need invoke here, because the winforms timer runs on the main thread.
Capturing();
}
timer1.Start();
}
正如@TheGeneral所说,保存后处理pictureBox2.Image。如果从不同线程访问对象,则需要一个锁。
当您从不同的线程访问控件时,也使用invoke
private readonly object myLock = new object();
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (myLock) {
this.Invoke((Action)(() => {
if (pictureBox1.Image != null)
((IDisposable)pictureBox1.Image).Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
// you can try with timer1 inside the lock
lock (myLock) {
// you don't need invoke here, because the winforms timer runs on the main thread.
Capturing();
}
timer1.Start();
}
正如@TheGeneral所说,保存后处理pictureBox2.Image。另一个进程正在使用的对象不是标准的BCL错误消息(据我所知)。您还存在其他问题,似乎您不处理位图而泄漏了位图,最终将耗尽GDI资源。如果它是线程安全的(取决于计时器的类型),“另一个进程正在使用的对象”不是标准的BCL错误消息(据我所知),这也会令人惊讶。您还存在其他问题,似乎您不处理位图而泄漏了位图,最终将耗尽GDI资源。如果它是线程安全的(取决于计时器的类型),这也会令人惊讶Ram泄漏已修复,但当代码在保存207个图像后运行时,它将停止保存任何内容,gui将变得无响应。@Terry24您是否尝试设置一些断点或Debug.WriteLine以查看其挂起的位置Ram泄漏已修复,但当代码在保存207个图像后运行时,它将停止保存任何内容,gui将变得无响应。@Terry24无响应..@Terry24是否尝试设置一些断点或Debug.WriteLine以查看挂起的位置