Web Cam Light is not closing

Hrishi Kuttathi 1 Reputation point
2021-01-24T08:30:23.647+00:00

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;
    }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,015 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.