This project is read-only.

Problems trying to stop and dispose camera

Nov 4, 2009 at 9:17 AM

Hi Tamir, thanks for this great code :)

I'm using your webcam control inside of different windows (but only one at the same time). So, I need to open a new windows, show images from the camera; close this windows and open it again. However, when I open again the windows, it doesn't work. I think it's some problem trying to dispose the camera resource.

An option that I've tried, but it doesn't work at all, is to stop the image feeding, and then, close the windows. In that case, when I open again the windows, it works. However, to stop the image feeding is very slow. It takes a long time to dispose the camera or it doesn't always do it.

In the next link, I've published a simple test VS project to show you what I'm trying to do:

http://cid-169823e2d3ac26bc.skydrive.live.com/self.aspx/Public/CODE/Custom%20Copy%20Base%20of%20AvCapWPF.zip

Note: I''ve tried with 3 different webcambs, and the result is the same in all of them.

Thank you very much!

Nov 18, 2011 at 11:08 AM

Hi there! Thank for this project, Tamir ^^

Did anyone manage to solve this problem? I'm facing it too...

Thanks a lot! :-)

Jul 26, 2013 at 1:24 PM
Hi,

Why don't you stop the player when you close the windows?
    public Window1()
    {
        InitializeComponent();

        this.Closed += (sender, args) => player.Stop();
    }
otherwise if you wanted, i find an upgrade of this solution :

http://blog.catenalogic.com/post/2008/post/2009/01/08/WPF-Webcam-Control-part-2.aspx
Dec 19, 2013 at 12:18 PM
Edited Dec 19, 2013 at 12:20 PM
Try
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
          {
              webcamPlayer.Device.Dispose();
          }        
It will work.
Apr 22, 2015 at 8:54 AM
I tried many way, so i find this way to Stop my Webcam Device. But i can't understand how it can do :)
Anyone can show me why?

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (webcamPlayer != null && webcamPlayer.Device != null)
        {
            webcamPlayer.Device.Stop();
            webcamPlayer.Device.Dispose();

            CommandBindings.Remove(new CommandBinding(Input.CaptureImageCommands.CaptureImage,
            new ExecutedRoutedEventHandler(CaptureImage_Executed), new CanExecuteRoutedEventHandler(CaptureImage_CanExecute)));
            CommandBindings.Remove(new CommandBinding(Input.CaptureImageCommands.RemoveImage,
                new ExecutedRoutedEventHandler(RemoveImage_Executed)));
            CommandBindings.Remove(new CommandBinding(Input.CaptureImageCommands.ClearAllImages,
                new ExecutedRoutedEventHandler(ClearAllImages_Executed)));

            SelectedImages.Clear();
            BitmapSource bitmap = webcamPlayer.CurrentBitmap;
            if (bitmap != null)
            {
                SelectedImages.Remove(bitmap);
            }
            //SelectedWebcamMonikerString =  "";
        }
    }