Web Cam Light is not closing
In my WPF c# application,there is a form using web camera for taking picture( forge.video).After closing the form it will not close the light. Below mentioned code is for stopping the webcam.Please help
public void Window_Closing(object sender, CancelEventArgs e)
{
Button_DeviceChooser_Click(sender, null);
if (FinalFrame.IsRunning == true)
{
try
{
this.Dispatcher.Invoke(() =>
{
FinalFrame.Stop();
//FinalFrame.NewFrame -= frameHandler; // click button event is fired,
//FinalFrame.VideoSourceError -= new VideoSourceErrorEventHandler(videoSource_Error);
//FinalFrame.NewFrame -= frameHandler; // click button event is fired,
//FinalFrame.VideoSourceError -= new VideoSourceErrorEventHandler(videoSource_Error);
//this.Dispatcher.InvokeShutdown();
//FinalFrame.SignalToStop();
//FinalFrame.WaitForStop();
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.ToString());
}
}
}
private void Button_DeviceChooser_Click(object sender, RoutedEventArgs e)
{
if (ComboBox_DeviceChooser.SelectedIndex < 0)
{
return;
}
FinalFrame = new VideoCaptureDevice(CaptureDevice[ComboBox_DeviceChooser.SelectedIndex].MonikerString); // specified web cam and its filter moniker string
try
{
//Check if the video device provides a list of supported resolutions
if (FinalFrame.VideoCapabilities.Length > 0)
{
string highestSolution = "0;0";
//Search for the highest resolution
for (int i = 0; i < FinalFrame.VideoCapabilities.Length; i++)
{
if (FinalFrame.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(';')[0]))
highestSolution = FinalFrame.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
}
//Set the highest resolution as active
highestSolution = "640;0";
FinalFrame.VideoResolution = FinalFrame.VideoCapabilities[Convert.ToInt32(highestSolution.Split(';')[1])];
}
}
catch (Exception ex)
{
if DEBUG
MessageBox.Show("Exception caught during resolution discovery.\r\n\r\n" +
ex.Message,
ex.GetType().ToString(),
MessageBoxButton.OK,
MessageBoxImage.Error);
endif
}
FinalFrame.NewFrame += frameHandler; // click button event is fired,
FinalFrame.VideoSourceError += new VideoSourceErrorEventHandler(videoSource_Error);
anchorPoint = new System.Drawing.Point();
selectionRectangle.Visibility = Visibility.Hidden;
ready = true;
Button_Crop.IsEnabled = false;
Button_Capture.IsEnabled = true;
Image_Previewer.Cursor = null;
FinalFrame.Start();
Image_Previewer.Height = 300;
// Remove crop handlers
Image_Previewer.MouseLeftButtonDown -= mouseEvent_ButtonDown;
Image_Previewer.MouseMove -= mouseEvent_Move;
Image_Previewer.MouseLeftButtonUp -= mouseEvent_ButtonUp;
}