title: Aforge.net Camera Capture & Save image to directory
body: <p>everyone. I have been stuck here dealing with this bugs for days, but I still couldn't figure it out. 
  My guess: I think my code has some problem as I did not dispose the object properly after using it, (I'm not very familiar with these concepts of releasing resources, threading) .
  I got these code by taking reference of what people did on youtube, but despite me doing exactly the same thing, my code didn't work out nicely.</p>
  
  <p>SITUATION:
  I have two picture boxes, left one can take video of me, right one take the snapshot, if you press button1 , you will start the video, clone_button will copy a image i.e. take a snapshot, and save_image should save it to the path reference, however, i get a generic error occured in GDI+ again and again while I'm trying to save it. Also, my debugger seemed to get crazy (i.e. failed to terminate the vshost.exe ) once I ran this program, I have to restart the computer to get my code running again, which is bleak and frustrating.</p>
  
  <pre><code>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.IO;
  using System.Drawing.Imaging;
  //AForge.Video dll
  using AForge.Video;
  using AForge.Video.DirectShow;
  using AForge.Imaging;
  using AForge.Imaging.Filters;
  using AForge;
  
  
  namespace WebCameraCapture
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
          private FilterInfoCollection CaptureDevice; // list of webcam
          private VideoCaptureDevice FinalFrame;
  
          private void Form1_Load(object sender, EventArgs e)
         {
              CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);//constructor
              foreach (FilterInfo Device in CaptureDevice)
              {
                  comboBox1.Items.Add(Device.Name);
              }
  
              comboBox1.SelectedIndex = 0; // default
              FinalFrame = new VideoCaptureDevice();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);// specified web cam and its filter moniker string
              FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);// click button event is fired, 
              FinalFrame.Start();
          }
  
          void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) // must be void so that it can be accessed everywhere.
      // New Frame Event Args is an constructor of a class
          {     
              pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();// clone the bitmap
          }
  
          private void From1_CLosing(object sender, EventArgs e)
          {
              if (FinalFrame.IsRunning==true) FinalFrame.Stop();
          }
  
          private void save_Click(object sender, EventArgs e)
          {
              if (pictureBox2.Image != null)
              {
                  Bitmap varBmp = new Bitmap(pictureBox2.Image);
                  Bitmap newBitmap = new Bitmap(varBmp);
                  varBmp.Dispose();
                  varBmp = null;
                  varBmp.Save(@"C:\a.png", ImageFormat.Png);
              }
              else
              { MessageBox.Show("null exception"); }
          }
  
          private void clone_Click(object sender, EventArgs e)
          {
              pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
          }
     }
  }
  </code></pre>
  
  <p>Any  AForge.net user can just PRESS the LINK below  and try it out. Thanks!</p>
  
  <p><a href="https://www.dropbox.com/s/zfdovr1wvs55aw4/WebCameraCapture.rar" rel="noreferrer">SAMPLE</a></p>