title: c# AForge.NET - how to save video from camera to file
body: <p>I need some help with saving video from laptop camera to file. Now i use code to search video devices and previous video on pictureBox.
  I've tried with AVIFileWrite but I failed. 
  How to use VideoFileWriter to achive my goal??</p>
  
  <pre><code>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 System.IO;
  using AForge.Video.VFW;
  using System.Drawing.Imaging;
  
  namespace VideoCapture
  {
      public partial class Form1 : Form
      {
          private FilterInfoCollection VideoCaptureDevices;
          private VideoCaptureDevice FinalVideo;
          public static Bitmap _latestFrame;
          //private VideoFileWriter writer;
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              VideoCaptureDevices = new                  FilterInfoCollection(FilterCategory.VideoInputDevice);
          foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
          {
              comboBox1.Items.Add(VideoCaptureDevice.Name);
          }
          comboBox1.SelectedIndex = 0;
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
  
          FinalVideo = new    VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
          FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
          FinalVideo.Start();
      }
      void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
      {
          Bitmap video = (Bitmap)eventArgs.Frame.Clone();
  
          pictureBox1.Image = video;
  
          //int width = 640;
          //int height = 480;
  
          //VideoFileWriter writer = new VideoFileWriter();
          //writer.Open(@"c:\video.avi", width, height, 25, VideoCodec.Default, 1000000);
  
          //for (int i = 0; i &lt; 1000; i++)
          //{
          //    writer.WriteVideoFrame(video);
          //}
  
          //writer.Close();
  
          //Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
          //writer.WriteVideoFrame(image);                      
          //MessageBox.Show("jest");           
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          if (FinalVideo.IsRunning)
          {
              FinalVideo.Stop();
  
          }
      }
  
      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
          if (FinalVideo.IsRunning == true) FinalVideo.Stop();
      }
  
      private void timer1_Tick(object sender, EventArgs e)
      {
          label1.Text = "Device running..." + FinalVideo.FramesReceived.ToString() + " FPS";
      }
  
  }
  </code></pre>
  
  <p>}</p>