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 System.Drawing.Imaging; using AForge.Video; using AForge.Video.DirectShow; using AForge.Vision.Motion; using AForge.Imaging; using System.Collections; namespace VideoPlayer { public partial class VideoPlayerForm : Form { private FileVideoSource videoSource; private int frameCount; private Boolean motionProcessing = false; private Boolean videoPaused = false; private MotionDetector motionDetector; public VideoPlayerForm() { InitializeComponent(); motionDetector = new MotionDetector(new SimpleBackgroundModelingDetector(true), new MotionBorderHighlighting(Color.Red)); } private void loadButton_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { loadVideoFromFile(openFileDialog1.FileName); } } private void loadVideoFromFile(String fileName) { videoSource = new FileVideoSource(fileName); // New frame event handler, which is invoked on each new available video frame videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); // videoSource.PlayingFinished += new PlayingFinishedEventHandler(video_Finished); } private void startButton_Click(object sender, EventArgs e) { frameCount = 0; videoSource.Start(); } private void pauseButton_Click(object sender, EventArgs e) { videoPaused = !videoPaused; } // New frame event handler, which is invoked on each new available video frame private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { // Get new frame Bitmap frame = (Bitmap)(eventArgs.Frame.Clone()); // Display the frame if (!motionProcessing) { if (!videoPaused) pictureBox1.Image = frame; } else { if (!videoPaused) { // Perform motion detection BitmapData data = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); UnmanagedImage unmanagedFrame = new UnmanagedImage(data); motionDetector.ProcessFrame(unmanagedFrame); UnmanagedImage motionFrame = motionDetector.MotionDetectionAlgorthm.MotionFrame; motionDetector.MotionProcessingAlgorithm.ProcessFrame(unmanagedFrame, motionFrame); Bitmap managedFrame = unmanagedFrame.ToManagedImage(); pictureBox1.Image = managedFrame; frame.UnlockBits(data); } } frameLabel.Text = "Frame: " + frameCount++; } private void motionDetectButton_Click(object sender, EventArgs e) { // Motion process frames motionProcessing = !motionProcessing; } private void stopButton_Click_1(object sender, EventArgs e) { videoSource.Stop(); } private void VideoPlayerForm_FormClosed(object sender, FormClosedEventArgs e) { videoSource.Stop(); } } }