public async Task Invoke(IReadOnlyDictionary <string, string> parameters, HSSettings settingsContext)
        {
            // TODO: If it is the first time the user invokes this command, we want to tell him that he can stop recording by pressing the same button again

            if (IsScreenRecorderRunning())
            {
                // We allow only one instance running. If the user invokes this task another time, he likely wanted to stop recording.
                // TODO: Add UI and trigger cancellation manually via mouse
                StopCurrentScreenRecorder(false);
                return;
            }

            var recording = await PerformScreenRecording(settingsContext);

            if (recording == null)
            {
                return; // User likely cancelled recording
            }
            // Every actionAfterCapture depends on the file in some way
            // Sometimes, it will be saved to a specified path
            // In that case, we want to first move the file to the target destination and then perform the action with that new path
            // Otherwise, we use the file in the temp dir

            if (settingsContext.SaveToLocalDisk)
            {
                var destDir = settingsContext.ExpandedSavePath;
                if (!Directory.Exists(destDir))
                {
                    IO.HolzShotsPaths.EnsureDirectory(destDir);
                }

                var extensionWithDot = Path.GetExtension(recording.FilePath);

                var targetFileName = FileNamePatternFormatter.GetFileNameFromPattern(
                    recording.GetMetadata(),
                    settingsContext.SaveVideoFileNamePattern
                    );

                var fullTargetFilePath = Path.Combine(destDir, targetFileName + extensionWithDot);

                FileEx.MoveAndRenameInsteadOfOverwrite(recording.FilePath, fullTargetFilePath);

                recording = recording with {
                    FilePath = fullTargetFilePath
                };
            }

            await InvokeAfterCaptureAction(recording, settingsContext);
        }
示例#2
0
        private static void SaveScreenshot(Screenshot shot, string ensuredDestinationDirectory, HSSettings settingsContext)
        {
            var format = ImageFormat.Png;
            var extensionAndMimeType = ImageFormatInformation.GetExtensionAndMimeType(format);

            Debug.Assert(shot.Image.GetType() == typeof(Bitmap));

            var screenshotImage = shot.Image.GetType() == typeof(Bitmap)
                ? shot.Image
                : new Bitmap(shot.Image);

            if (settingsContext.EnableSmartFormatForSaving && ImageFormatAnalyser.IsOptimizable(screenshotImage))
            {
                format = ImageFormatAnalyser.GetBestFittingFormat(screenshotImage);

                extensionAndMimeType = format.GetExtensionAndMimeType();
                Debug.Assert(!string.IsNullOrWhiteSpace(extensionAndMimeType.FileExtension));
            }

            var pattern     = settingsContext.SaveImageFileNamePattern;
            var patternData = shot.GetFileMetadata(format);

            string name;

            try
            {
                name = FileNamePatternFormatter.GetFileNameFromPattern(patternData, pattern);
            }
            catch (PatternSyntaxException)
            {
                NotificationManager.InvalidFilePattern(pattern);
                return;
            }

            var fileName = Path.ChangeExtension(name, extensionAndMimeType.FileExtension);
            var path     = GetAbsolutePath(ensuredDestinationDirectory, fileName);

            var freePath = FileEx.GetUnusedFileNameFromCandidate(path);

            screenshotImage.Save(freePath, format);

            _lastFileName = path;
        }