Exemplo 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);
            }
        }
Exemplo n.º 2
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);
                }
            }
        }