private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //Timeline.DesiredFrameRateProperty.OverrideMetadata(
                //    typeof(Timeline),
                //    new FrameworkPropertyMetadata { DefaultValue = this.selectedVideoSource.VideoResolution.AverageFrameRate }
                //    );

                //Timeline.DesiredFrameRateProperty.OverrideMetadata(
                //    typeof(Timeline),
                //    new FrameworkPropertyMetadata { DefaultValue = 10 }
                //    );

                this.uiThread = Application.Current.Dispatcher;
                this.Wbmp = null;
                //this.sw = new Stopwatch();

                this.videoSources = null;
                this.selectedVideoSource = null;
                this.resolutions = null;
                this.waitForStopInterval = 500;

                //this.NewFrameHandler = new Action(this.NewFrame);
                this.newFrameLock = new object();
                this.newFrameSubscribeCount = 0;
                this.stopRequested = false;
                this.bypassSelectionChanged = true;
                this.lastSuccessfulVideoSourceIdx = 0;
                this.lastSuccessfulResolutionIdx = 0;
                this.autoHiResSnapshotRequested = false;
                this.snapshotRequested = false;
                this.flashRequested = false;
                this.flashIsOn = false;
                this.waitForAutoFocus = 0;
                this.waitForAutoFocusLimit = 2;
                this.frameCount = 0;
                this.snapshotCaptured = false;
                this.clickPlayer = new MediaPlayer();
                this.clickPlayer.Volume = 1;
                this.clickPlayer.Open(new Uri(@"Media\Click.wav", UriKind.Relative));
                this.lastSavedFilename = string.Empty;

                // Initialize the Getac flash DLL
                AforgeCameraWPF.GetacDLLWrapper.InitGetacDLL();
                Log.Debug("getac DLL initialized");

                // Get all available video sources.  Note, this call
                // populates the list of available video sources if
                // there are any.

                this.videoSources = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                // Check if atleast one video source is available
                if (this.videoSources != null && this.videoSources.Count > 0)
                {
                    // Bind the video sources combobox
                    Binding videoSourcesBinding = new Binding();
                    videoSourcesBinding.Source = this.videoSources;

                    this.VideoSourcesComboBox.DisplayMemberPath = "Name";
                    this.VideoSourcesComboBox.SelectedValuePath = "MonikerString";
                    this.VideoSourcesComboBox.SetBinding(ComboBox.ItemsSourceProperty, videoSourcesBinding);
                    this.VideoSourcesComboBox.SelectionChanged += VideoSourcesComboBox_SelectionChanged;

                    // Use the first video device.
                    this.selectedVideoSource = new VideoCaptureDevice(videoSources[0].MonikerString);
                    this.VideoSourcesComboBox.SelectedIndex = 0;

                    // Initialize the video source and start it.
                    this.InitializeSelectedVideoSource();
                }

                // Disable the save button.
                this.SaveSnapshotButton.IsEnabled = false;
                this.SaveSnapshotBottomButton.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            finally
            {
                this.bypassSelectionChanged = false;
            }
        }
        private void InitializeSelectedVideoSource()
        {
            try
            {
                if (this.videoSources == null)
                {
                    Log.Debug("Initialize bypassed - video sources is null");
                    return;
                }

                if (this.videoSources.Count == 0)
                {
                    Log.Debug("Initialize bypassed - video sources count is zero.");
                    return;
                }

                if (this.selectedVideoSource == null)
                {
                    Log.Debug("Initialize bypassed - selected video source is null.");
                    return;
                }

                if (this.selectedVideoSource.IsRunning)
                {
                    Log.Debug("Initialize bypassed - selected video source is running.");
                    return;
                }

                if (this.selectedVideoSource.VideoCapabilities == null)
                {
                    Log.Debug("Initialize bypassed - selected video source capabilities is null.");
                    return;
                }

                if (this.selectedVideoSource.VideoCapabilities.Length == 0)
                {
                    Log.Debug("Initialize bypassed - selected video source capabilities length is zero.");
                    return;
                }

                Log.Debug("Initializing video source");

                Binding resolutionsBinding = null;
                if (this.resolutions == null)
                {
                    // First time in.

                    Log.Debug("Creating resolution collection");
                    this.resolutions = new CameraResolutions();
                    this.resolutions.ResetItems(this.selectedVideoSource.VideoCapabilities);

                    // Bind the resolutions combobox
                    Log.Debug("Binding resolutions combobox");
                    resolutionsBinding = new Binding();
                    resolutionsBinding.Source = this.resolutions.Items;
                    this.ResolutionComboBox.DisplayMemberPath = "Display";
                    this.ResolutionComboBox.SelectedValuePath = "Id";
                    this.ResolutionComboBox.SetBinding(ComboBox.ItemsSourceProperty, resolutionsBinding);
                    this.ResolutionComboBox.SelectionChanged += this.ResolutionComboBox_SelectionChanged;
                }
                else
                {
                    Log.Debug("Clearing resolutions binding");
                    BindingOperations.ClearBinding(this.ResolutionComboBox, ComboBox.ItemsSourceProperty);
                    Log.Debug("Resetting resolution collection");
                    this.resolutions.ResetItems(this.selectedVideoSource.VideoCapabilities);
                    Log.Debug("Rebinding resolutions combobox");
                    resolutionsBinding = new Binding();
                    resolutionsBinding.Source = this.resolutions.Items;
                    this.ResolutionComboBox.SetBinding(ComboBox.ItemsSourceProperty, resolutionsBinding);
                }

                // Select the highest resolution
                //int highestRes = this.resolutions.IndexOfHighestResolution();
                //this.ResolutionComboBox.SelectedIndex = highestRes;
                //this.selectedVideoSource.VideoResolution = selectedVideoSource.VideoCapabilities[highestRes];
                // todo selecting the first resolution so that I can test the high res snapshot approach.

                this.bypassSelectionChanged = true;
                this.ResolutionComboBox.SelectedIndex = viewerResolutionIndexDefault;
                this.selectedVideoSource.VideoResolution = selectedVideoSource.VideoCapabilities[viewerResolutionIndexDefault];
                this.bypassSelectionChanged = false;

                // Start the video source.
                Log.Debug("Starting selected video source");
                this.StartSelectedVideoSource();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }