Пример #1
0
        void StartRecording()
        {
            var SelectedAudioSourceId   = AudioSettings.SelectedAudioSourceId;
            var SelectedVideoSourceKind = VideoSettings.SelectedVideoSourceKind;
            var SelectedVideoSource     = VideoSettings.SelectedVideoSource;
            var Encoder = VideoSettings.Encoder;

            Duration = OtherSettings.CaptureDuration;
            Delay    = OtherSettings.StartDelay;

            if (Duration != 0 && (Delay * 1000 > Duration))
            {
                Status.Content = "Delay cannot be greater than Duration";
                SystemSounds.Asterisk.Play();
                return;
            }

            if (OtherSettings.MinimizeOnStart)
            {
                WindowState = WindowState.Minimized;
            }

            VideoSettings.Instance.VideoSourceKindBox.IsEnabled = false;
            VideoSettings.Instance.VideoSourceBox.IsEnabled     = SelectedVideoSourceKind == VideoSourceKind.Window;

            // UI Buttons
            RecordButton.ToolTip  = "Stop";
            RecordButton.IconData = (RectangleGeometry)FindResource("StopIcon");

            ReadyToRecord = false;

            int temp;

            string Extension = SelectedVideoSourceKind == VideoSourceKind.NoVideo
                ? (AudioSettings.EncodeAudio && int.TryParse(SelectedAudioSourceId, out temp) ? ".mp3" : ".wav")
                : (Encoder.Name == "Gif" ? ".gif" : ".avi");

            lastFileName = Path.Combine(OutPath.Text, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + Extension);

            Status.Content = Delay > 0 ? string.Format("Recording from t={0}ms...", Delay) : "Recording...";

            DTimer.Stop();
            Seconds             = Minutes = 0;
            TimeManager.Content = "00:00";

            DTimer.Start();

            int AudioBitRate = App.IsLamePresent ? Mp3EncoderLame.SupportedBitRates[AudioSettings.AudioQuality] : 0;

            IAudioProvider AudioSource = null;
            WaveFormat     wf          = new WaveFormat(44100, 16, AudioSettings.Stereo ? 2 : 1);

            if (SelectedAudioSourceId != "-1")
            {
                int i;
                if (int.TryParse(SelectedAudioSourceId, out i))
                {
                    AudioSource = new WaveIn(i, VideoSettings.FrameRate, wf);
                }
                else
                {
                    AudioSource = new WasapiLoopbackCapture(WasapiAudioDevice.Get(SelectedAudioSourceId), true);
                    wf          = AudioSource.WaveFormat;
                }
            }

            #region ImageProvider
            IImageProvider ImgProvider = null;

            Func <System.Windows.Media.Color, System.Drawing.Color> ConvertColor = (C) => System.Drawing.Color.FromArgb(C.A, C.R, C.G, C.B);

            var mouseKeyHook = new MouseKeyHook(OtherSettings.CaptureClicks,
                                                OtherSettings.CaptureKeystrokes);

            if (SelectedVideoSourceKind == VideoSourceKind.Window)
            {
                var Src = SelectedVideoSource as WindowVSLI;

                if (Src.Handle == RegionSelector.Instance.Handle &&
                    OtherSettings.StaticRegionCapture)
                {
                    ImgProvider = new StaticRegionProvider(RegionSelector.Instance,
                                                           cursor,
                                                           mouseKeyHook);
                    VideoSettings.Instance.VideoSourceBox.IsEnabled = false;
                }
                else
                {
                    ImgProvider = new WindowProvider(() => (VideoSettings.SelectedVideoSource as WindowVSLI).Handle,
                                                     ConvertColor(VideoSettings.BackgroundColor),
                                                     cursor,
                                                     mouseKeyHook);
                }
            }
            else if (SelectedVideoSourceKind == VideoSourceKind.Screen)
            {
                ImgProvider = new ScreenProvider((SelectedVideoSource as ScreenVSLI).Screen,
                                                 cursor,
                                                 mouseKeyHook);
            }
            #endregion

            #region VideoEncoder
            IVideoFileWriter VideoEncoder = null;

            if (Encoder.Name == "Gif")
            {
                if (GifSettings.UnconstrainedGif)
                {
                    Recorder = new UnconstrainedFrameRateGifRecorder(
                        new GifWriter(lastFileName,
                                      Repeat: GifSettings.GifRepeat ? GifSettings.GifRepeatCount : -1),
                        ImgProvider);
                }

                else
                {
                    VideoEncoder = new GifWriter(lastFileName, 1000 / VideoSettings.FrameRate,
                                                 GifSettings.GifRepeat ? GifSettings.GifRepeatCount : -1);
                }
            }

            else if (SelectedVideoSourceKind != VideoSourceKind.NoVideo)
            {
                VideoEncoder = new AviWriter(lastFileName,
                                             ImgProvider,
                                             Encoder,
                                             VideoSettings.VideoQuality,
                                             VideoSettings.FrameRate,
                                             AudioSource,
                                             AudioBitRate == 0 ? null
                                                                : new Mp3EncoderLame(wf.Channels, wf.SampleRate, AudioBitRate));
            }
            #endregion

            if (Recorder == null)
            {
                if (SelectedVideoSourceKind == VideoSourceKind.NoVideo)
                {
                    if (AudioSettings.EncodeAudio)
                    {
                        Recorder = new AudioRecorder(AudioSource, new EncodedAudioFileWriter(lastFileName, new Mp3EncoderLame(wf.Channels, wf.SampleRate, AudioBitRate)));
                    }
                    else
                    {
                        Recorder = new AudioRecorder(AudioSource, new WaveFileWriter(lastFileName, wf));
                    }
                }
                else
                {
                    Recorder = new Recorder(VideoEncoder, ImgProvider, AudioSource);
                }
            }

            Recorder.RecordingStopped += (E) => Dispatcher.Invoke(() =>
            {
                OnStopped();

                if (E != null)
                {
                    Status.Content = "Error";
                    MessageBox.Show(E.ToString());
                }
            });

            Recorder.Start(Delay);

            Recent.Add(lastFileName,
                       VideoEncoder == null ? RecentItemType.Audio : RecentItemType.Video);
        }
Пример #2
0
        void CaptureScreenShot(object sender = null, RoutedEventArgs e = null)
        {
            if (!Directory.Exists(OutPath.Text))
            {
                Directory.CreateDirectory(OutPath.Text);
            }

            string      FileName  = null;
            ImageFormat ImgFmt    = ScreenShotSettings.SelectedImageFormat;
            string      Extension = ImgFmt == ImageFormat.Icon ? "ico"
                : ImgFmt == ImageFormat.Jpeg ? "jpg"
                : ImgFmt.ToString().ToLower();
            bool SaveToClipboard = ScreenShotSettings.SaveToClipboard;

            if (!SaveToClipboard)
            {
                FileName = Path.Combine(OutPath.Text,
                                        DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "." + Extension);
            }

            Bitmap BMP = null;

            var SelectedVideoSourceKind = VideoSettings.SelectedVideoSourceKind;
            var SelectedVideoSource     = VideoSettings.SelectedVideoSource;
            var IncludeCursor           = Properties.Settings.Default.IncludeCursor;

            if (SelectedVideoSourceKind == VideoSourceKind.Window)
            {
                IntPtr hWnd = (SelectedVideoSource as WindowVSLI).Handle;

                if (hWnd == WindowProvider.DesktopHandle)
                {
                    BMP = ScreenShot.Capture(IncludeCursor);
                }
                else if (hWnd == RegionSelector.Instance.Handle)
                {
                    BMP = ScreenShot.Capture(RegionSelector.Instance.Rectangle, IncludeCursor);
                }
                else
                {
                    BMP = ScreenShot.CaptureTransparent(hWnd, IncludeCursor,
                                                        ScreenShotSettings.DoResize, ScreenShotSettings.ResizeWidth, ScreenShotSettings.ResizeHeight);
                }
            }
            else if (SelectedVideoSourceKind == VideoSourceKind.Screen)
            {
                BMP = (SelectedVideoSource as ScreenVSLI).Capture(IncludeCursor);
            }

            // Save to Disk or Clipboard
            if (BMP != null)
            {
                if (SaveToClipboard)
                {
                    BMP.WriteToClipboard(ImgFmt == ImageFormat.Png);
                    Status.Content = "Image Saved to Clipboard";
                }
                else
                {
                    try
                    {
                        BMP.Save(FileName, ImgFmt);
                        Status.Content = "Image Saved to Disk";
                    }
                    catch (Exception E)
                    {
                        Status.Content = "Not Saved. " + E.Message;
                        return;
                    }
                }

                if (FileName != null && !SaveToClipboard)
                {
                    Recent.Add(FileName, RecentItemType.Image);
                }
            }
            else
            {
                Status.Content = "Not Saved - Image taken was Empty";
            }
        }