示例#1
0
        public void Save(Bitmap Image, ImageFormat Format, string FileName, TextLocalizer Status, RecentViewModel Recents)
        {
            try
            {
                Settings.Instance.EnsureOutPath();

                var extension = Format.Equals(ImageFormat.Icon) ? "ico"
                    : Format.Equals(ImageFormat.Jpeg) ? "jpg"
                    : Format.ToString().ToLower();

                var fileName = FileName ?? Path.Combine(Settings.Instance.OutPath, $"{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.{extension}");

                using (Image)
                    Image.Save(fileName, Format);

                Status.LocalizationKey = nameof(Resources.ImgSavedDisk);
                Recents.Add(fileName, RecentItemType.Image, false);

                if (Settings.Instance.CopyOutPathToClipboard)
                {
                    fileName.WriteToClipboard();
                }

                ServiceProvider.SystemTray.ShowScreenShotNotification(fileName);
            }
            catch (Exception E)
            {
                ServiceProvider.MessageProvider.ShowError($"{nameof(Resources.NotSaved)}\n\n{E}");

                Status.LocalizationKey = nameof(Resources.NotSaved);
            }
        }
示例#2
0
        public async Task Save(Bitmap Image, ImageFormat Format, string FileName, RecentViewModel Recents)
        {
            var response = await Save(Image, Format);

            switch (response)
            {
            case ImgurUploadResponse uploadResponse:
                var recentItem = Recents.Add(uploadResponse.Data.Link, RecentItemType.Link, false);
                recentItem.DeleteHash = uploadResponse.Data.DeleteHash;

                // Copy path to clipboard only when clipboard writer is off
                if (_settings.CopyOutPathToClipboard && !ServiceProvider.Get <ClipboardWriter>().Active)
                {
                    uploadResponse.Data.Link.WriteToClipboard();
                }
                break;

            case Exception e:
                if (!_diskWriter.Active)
                {
                    ServiceProvider.Get <IMainWindow>().IsVisible = true;

                    var yes = _messageProvider.ShowYesNo(
                        $"{_loc.ImgurFailed}\n{e.Message}\n\nDo you want to Save to Disk?", "Imgur Upload Failed");

                    if (yes)
                    {
                        await _diskWriter.Save(Image, Format, FileName, Recents);
                    }
                }
                break;
            }
        }
示例#3
0
        public Task Save(Bitmap Image, ImageFormat Format, string FileName, RecentViewModel Recents)
        {
            try
            {
                _settings.EnsureOutPath();

                var extension = Format.Equals(ImageFormat.Icon) ? "ico"
                    : Format.Equals(ImageFormat.Jpeg) ? "jpg"
                    : Format.ToString().ToLower();

                var fileName = FileName ?? Path.Combine(_settings.OutPath, $"{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.{extension}");

                Image.Save(fileName, Format);

                Recents.Add(fileName, RecentItemType.Image, false);

                // Copy path to clipboard only when clipboard writer is off
                if (_settings.CopyOutPathToClipboard && !ServiceProvider.Get <ClipboardWriter>().Active)
                {
                    fileName.WriteToClipboard();
                }

                _systemTray.ShowScreenShotNotification(fileName);
            }
            catch (Exception e)
            {
                _messageProvider.ShowError($"{nameof(LanguageManager.NotSaved)}\n\n{e}");
            }

            return(Task.CompletedTask);
        }
示例#4
0
        public Task Save(Bitmap Image, ImageFormat Format, string FileName, RecentViewModel Recents)
        {
            try
            {
                _settings.EnsureOutPath();

                var extension = Format.Equals(ImageFormat.Icon) ? "ico"
                    : Format.Equals(ImageFormat.Jpeg) ? "jpg"
                    : Format.ToString().ToLower();

                var fileName = _settings.GetFileName(extension, FileName);

                Image.Save(fileName, Format);

                Recents.Add(fileName, RecentItemType.Image, false);

                // Copy path to clipboard only when clipboard writer is off
                if (_settings.CopyOutPathToClipboard && !ServiceProvider.Get <ClipboardWriter>().Active)
                {
                    fileName.WriteToClipboard();
                }

                _systemTray.ShowScreenShotNotification(fileName);
            }
            catch (Exception e)
            {
                _messageProvider.ShowException(e, _loc.NotSaved);
            }

            return(Task.CompletedTask);
        }
示例#5
0
        public async void Save(Bitmap Image, ImageFormat Format, string FileName, TextLocalizer Status, RecentViewModel Recents)
        {
            var ritem = Recents.Add($"{_loc.ImgurUploading} (0%)", RecentItemType.Link, true);

            using (var w = new WebClient {
                Proxy = _settings.Proxy.GetWebProxy()
            })
            {
                w.UploadProgressChanged += (s, e) =>
                {
                    ritem.Display = $"{_loc.ImgurUploading} ({e.ProgressPercentage}%)";
                };

                w.Headers.Add("Authorization", $"Client-ID {ApiKeys.ImgurClientId}");

                NameValueCollection values;

                using (var ms = new MemoryStream())
                {
                    Image.Save(ms, Format);

                    values = new NameValueCollection
                    {
                        { "image", Convert.ToBase64String(ms.ToArray()) }
                    };
                }

                XDocument xdoc;

                try
                {
                    var response = await w.UploadValuesTaskAsync("https://api.imgur.com/3/upload.xml", values);

                    xdoc = XDocument.Load(new MemoryStream(response));

                    var xAttribute = xdoc.Root?.Attribute("success");

                    if (xAttribute == null || int.Parse(xAttribute.Value) != 1)
                    {
                        throw new Exception("Response indicates Failure");
                    }

                    Image.Dispose();
                }
                catch (Exception E)
                {
                    ritem.Display          = _loc.ImgurFailed;
                    Status.LocalizationKey = nameof(LanguageManager.ImgurFailed);

                    var yes = _messageProvider.ShowYesNo($"{_loc.ImgurFailed}\n{E.Message}\n\nDo you want to Save to Disk?", "Imgur Upload Failed");

                    if (yes)
                    {
                        _diskWriter.Save(Image, Format, FileName, Status, Recents);
                    }

                    return;
                }

                var link = xdoc.Root.Element("link").Value;

                if (_settings.CopyOutPathToClipboard)
                {
                    link.WriteToClipboard();
                }

                ritem.FilePath = ritem.Display = link;
                ritem.Saved();

                _systemTray.ShowTextNotification($"{_loc.ImgurSuccess}: {link}", _settings.UI.ScreenShotNotifyTimeout, () => Process.Start(link));

                Status.LocalizationKey = nameof(LanguageManager.ImgurSuccess);
            }
        }
示例#6
0
        public async Task Save(Bitmap Image, ImageFormat Format, string FileName, RecentViewModel Recents)
        {
            var progressItem = _systemTray.ShowProgress();

            progressItem.PrimaryText = _loc.ImgurUploading;

            using (var w = new WebClient {
                Proxy = _settings.Proxy.GetWebProxy()
            })
            {
                w.UploadProgressChanged += (s, e) =>
                {
                    progressItem.Progress = e.ProgressPercentage;
                };

                w.Headers.Add("Authorization", $"Client-ID {ApiKeys.ImgurClientId}");

                NameValueCollection values;

                using (var ms = new MemoryStream())
                {
                    Image.Save(ms, Format);

                    values = new NameValueCollection
                    {
                        { "image", Convert.ToBase64String(ms.ToArray()) }
                    };
                }

                XDocument xdoc;

                try
                {
                    var response = await w.UploadValuesTaskAsync("https://api.imgur.com/3/upload.xml", values);

                    xdoc = XDocument.Load(new MemoryStream(response));

                    var xAttribute = xdoc.Root?.Attribute("success");

                    if (xAttribute == null || int.Parse(xAttribute.Value) != 1)
                    {
                        throw new Exception("Response indicates Failure");
                    }
                }
                catch (Exception e)
                {
                    progressItem.Finished = true;
                    progressItem.Success  = false;

                    progressItem.PrimaryText = _loc.ImgurFailed;

                    if (!_diskWriter.Active)
                    {
                        ServiceProvider.Get <IMainWindow>().IsVisible = true;

                        var yes = _messageProvider.ShowYesNo(
                            $"{_loc.ImgurFailed}\n{e.Message}\n\nDo you want to Save to Disk?", "Imgur Upload Failed");

                        if (yes)
                        {
                            await _diskWriter.Save(Image, Format, FileName, Recents);
                        }
                    }

                    return;
                }

                var link = xdoc.Root.Element("link").Value;

                // Copy path to clipboard only when clipboard writer is off
                if (_settings.CopyOutPathToClipboard && !ServiceProvider.Get <ClipboardWriter>().Active)
                {
                    link.WriteToClipboard();
                }

                Recents.Add(link, RecentItemType.Link, false);

                progressItem.Finished      = true;
                progressItem.Success       = true;
                progressItem.PrimaryText   = _loc.ImgurSuccess;
                progressItem.SecondaryText = link;

                progressItem.RegisterClick(() => Process.Start(link));
            }
        }