Esempio n. 1
0
        public async Task Invoke(IReadOnlyDictionary <string, string> parameters, HSSettings settingsContext)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (settingsContext == null)
            {
                throw new ArgumentNullException(nameof(settingsContext));
            }

            var fileName = parameters.Count != 1 || !parameters.ContainsKey(FileNameParameter) ? ShowFileSelector(UploadFile) : parameters[FileNameParameter];

            if (fileName == null)
            {
                return; // We did not get a valid file name (user cancelled or something else was strange)
            }
            if (!CanProcessFile(fileName))
            {
                // TODO: Error Message
                return;
            }

            using var bmp = new Bitmap(fileName);

            var format = ImageFormatInformation.GetImageFormatFromFileName(fileName);

            Debug.Assert(format != null);

            try
            {
                var result = await UploadDispatcher.InitiateUploadToDefaultUploader(bmp, settingsContext, HolzShotsApplication.Instance.Uploaders, format, null).ConfigureAwait(true);

                UploadHelper.InvokeUploadFinishedUI(result, settingsContext);
            }
            catch (UploadCanceledException)
            {
                NotificationManager.ShowOperationCanceled();
            }
            catch (UploadException ex)
            {
                await NotificationManager.UploadFailed(ex);
            }
        }
Esempio n. 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;
        }
Esempio n. 3
0
        private static void PromptSaveAs(Screenshot screenshot, HSSettings settingsContext)
        {
            // TODO: Move this
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter          = $"{Localization.PngImage}|*.png|{Localization.JpgImage}|*.jpg";
                sfd.DefaultExt      = ".png";
                sfd.CheckPathExists = true;
                sfd.Title           = Localization.ChooseDestinationFileName;
                var res = sfd.ShowDialog();
                if (res != DialogResult.OK)
                {
                    return;
                }

                var f = sfd.FileName;
                if (string.IsNullOrWhiteSpace(f))
                {
                    return;
                }

                var format = ImageFormatInformation.GetImageFormatFromFileName(f);
                Debug.Assert(format != null);

                try
                {
                    using var fileStream = System.IO.File.OpenWrite(f);
                    screenshot.Image.SaveExtended(fileStream, format);
                }
                catch (PathTooLongException)
                {
                    NotificationManager.PathIsTooLong(f);
                }
                catch (Exception ex)
                {
                    NotificationManager.ErrorSavingImage(ex);
                }
            }
        }
Esempio n. 4
0
        private static ImageFormatInfo GetImageFormatInfoFromFileName(string fileName)
        {
            var extension = Path.GetExtension(fileName).TrimStart('.').ToLower();

            return(ImageFormatInformation.Single(x => x.Value.Extension == extension).Value);
        }