MathewSachin / Captura

Capture Screen, Audio, Cursor, Mouse Clicks and Keystrokes
https://mathewsachin.github.io/Captura/
MIT License
9.81k stars 1.84k forks source link

Webcam recording with v8.0 #542

Closed postacik closed 4 years ago

postacik commented 4 years ago

Hi,

I used the Captura.Console sample as a base application and created a Winforms application with Captura libraries.

When I run the Captura.Console app with "--webcam 1" option, it can capture the webcam without any problems.

But when I do the same in my Winforms application with the following lines, webcam is not recorded.

                if (_webCamProvider.AvailableCams.Count > 1)
                {
                    _webCamProvider.SelectedCam = _webCamProvider.AvailableCams[1];
                    Thread.Sleep(500);
                } 

Do you have any idea why this may happen?

The Webcam library always creates a preview window when SelectedCam is set.

In your app there's always a tiny window at the top which shows the webcam view when a webcam is selected.

Should I do the same in my Winforms application?

postacik commented 4 years ago

When I set the option to set the webcam recording to a separate file, the recorded file is just black and empty.

I noticed that the library gives the following exception:

Exception thrown: 'System.InvalidCastException' in Captura.Webcam

...in the following function:

        public Bitmap Capture()
        {
            try
            {
                return _captureWebcam?.GetFrame();
            }
            catch { return null; }
        }
postacik commented 4 years ago

In the FakesModule you bind CoreWebCamProvider but in the Captura app you bind WebCamProvider.

Should I be doing something similar?

postacik commented 4 years ago

I start recording from the thread of the main form of my application. Can this be the reason?

                    Utils.MainForm.Invoke(new MethodInvoker(delegate
                    {
                        Recorder.StartRecording(windowTitle);
                    }));
postacik commented 4 years ago

I solved it by changing the following CoreWebCamProvider methods:

        public Bitmap Capture()
        {
            try
            {
                return (Bitmap)_previewForm.Invoke(new Func<Bitmap>(() => _captureWebcam?.GetFrame()));
            }
            catch {
                return null;
            }
        }

        public int Width => (int)_previewForm.Invoke(new Func<int>(() => _captureWebcam.Size.Width));

        public int Height => (int)_previewForm.Invoke(new Func<int>(() => _captureWebcam.Size.Height));