我可以将网络摄像头或任何其他集成设备中的视频显示到图片框中。此外,我可以使用FFMPEG DLL文件将视频保存到avi文件中。 我想同时做两件事,即将视频保存在avi文件中,同时也显示实时源。 这是一个监控项目,我想监控实时信息并保存它们。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
using System.Drawing.Imaging;
using System.IO;
namespace cam_aforge1
{
public partial class Form1 : Form
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
public Form1()
{
InitializeComponent();
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
private void rfsh_Click(object sender, EventArgs e)
{
getCamList();
}
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrameSave);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
label2.Text = "Device running...";
start.Text = "&Stop";
timer1.Enabled = true;
}
else
{
label2.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
timer1.Enabled = false;
CloseVideoSource();
label2.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = img;
}
Bitmap imgsave;
private void video_NewFrameSave(object sender, NewFrameEventArgs eventArgs)
{
imgsave = (Bitmap)eventArgs.Frame.Clone();
}
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseVideoSource();
}
private void Form1_Load(object sender, EventArgs e)
{
}
VideoFileWriter writer;
private void button1_Click(object sender, EventArgs e)
{
int width = 640;
int height = 480;
writer = new VideoFileWriter();
writer.Open("test.avi", width, height, 75, VideoCodec.MPEG4);
for (int i = 0; i < 5000; i++)
{
writer.WriteVideoFrame(imgsave);
}
}
private void button2_Click(object sender, EventArgs e)
{
writer.Close();
}
}
}
提前致谢。