AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架。它包括图像处理、神经网络、遗传算法和机器学习等。
我利用它搞的程序现在能同时显示两路摄像头视频,但只能保存一路视频的图像,有用过AForge.NET的吗?请问
如何正确保存图像.如果不设flag标志,它会一直保存起来不停.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
//
namespace Camera
{
public partial class Form1 : Form
{
private int flag = 1;
private int flag2=1;
private VideoCaptureDevice videoSource1;
private VideoCaptureDevice videoSource2;
private FilterInfoCollection videoDevices;
//修改构造函数
public Form1()
{
InitializeComponent();
//让委托变量等于"设置标签的文本"的方法
}
public delegate void setLabelTextDelegate(); //定义一个委托
private setLabelTextDelegate BrowsLable; //定义这个委托的一个变量
private void ChengText()
{
this.label1.Text = "两路摄像头驱动成功!";
}
private void ThreadMethod()
{
this.label1.Invoke(BrowsLable); //括号里面是上面定义的委托变量
}
private void Form1_Load(object sender, EventArgs e)
{
//新建立一个线程
System.Threading.Thread setLabelTextThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadMethod));
this.BrowsLable = this.ChengText; //让委托变量等于"设置标签的文本"的方法
setLabelTextThread.Start();
try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException(); //抛出错误信息
foreach (FilterInfo device in videoDevices)
{
tscbxCameras.Items.Add(device.Name);
}
tscbxCameras.SelectedIndex = 0;
}
catch (ApplicationException)
{
tscbxCameras.Items.Add("摄像头没有连接!");
videoDevices = null;
}
//连接摄像头1
CameraConn1();
//连接摄像头2
CameraConn2();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
CameraConn1();
}
private void CameraConn1()
{
videoSource1 = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoSource1.DesiredFrameSize = new Size(320, 240);
videoSource1.DesiredFrameRate = 15;
videPlayer.VideoSource = videoSource1;
videPlayer.Start();
}
private void CameraConn2()
{
videoSource2 = new VideoCaptureDevice(videoDevices[1].MonikerString);
videoSource2.DesiredFrameSize = new Size(320, 240);
videoSource2.DesiredFrameRate = 15;
videPlayer1.VideoSource = videoSource2;
videPlayer1.Start();
}
//停止摄像头
private void toolStripButton2_Click(object sender, EventArgs e)
{
videPlayer.SignalToStop();
videPlayer.WaitForStop();
videPlayer1.SignalToStop();
videPlayer1.WaitForStop();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
toolStripButton2_Click(null, null);
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (flag ==1)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
try
{
lock (this)
{
string path = "c:\\" + DateTime.Now.ToString("MMddhhmmss") + ".bmp";
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
flag = 0;
}
}
catch (Exception ex)
{
MessageBox.Show("保存图像失败!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void video_NewFrame2(object sender, NewFrameEventArgs eventArgs)
{
if (flag2 == 1)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
try
{
lock (this)
{
string path="c:\\" + DateTime.Now.ToString("MMddhhmmss")+".bmp";
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
flag2= 0;
}
}
catch (Exception ex)
{
MessageBox.Show("保存图像失败!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button1_Click(object sender, Form1 eventArgs)
{
}
//保存图片
private void button1_Click(object sender, EventArgs e)
{
while (flag == 1)
{
videoSource1.NewFrame += new NewFrameEventHandler(video_NewFrame);
}
while (flag2 == 1)
{
videoSource2.NewFrame += new NewFrameEventHandler(video_NewFrame2);
}
label1.Text = "拍照成功,完成时间:" + DateTime.Now.ToString("yyyyMMddhhmmss");
}
private void button2_Click(object sender, EventArgs e)
{
videoSource1.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
videoSource1.Start();
}
// New frame received by the player //在视频上写文字
private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
DateTime now = DateTime.Now;
Graphics g = Graphics.FromImage(image);
// paint current time
SolidBrush brush = new SolidBrush(Color.Red);
g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
brush.Dispose();
g.Dispose();
}
// New frame received by the player //在视频上写文字
private void videoSourcePlayer_NewFrame2(object sender, ref Bitmap image)
{
DateTime now = DateTime.Now;
Graphics g = Graphics.FromImage(image);
// paint current time
SolidBrush brush = new SolidBrush(Color.Red);
g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
brush.Dispose();
g.Dispose();
}
}
}