// Returns ImgurUploadResponse on success, Exception on failure public async Task <object> Save(Bitmap Image, ImageFormat Format) { 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; }; try { w.Headers.Add("Authorization", await GetAuthorizationHeader()); } catch (Exception e) { return(e); } NameValueCollection values; using (var ms = new MemoryStream()) { Image.Save(ms, Format); values = new NameValueCollection { { "image", Convert.ToBase64String(ms.ToArray()) } }; } ImgurUploadResponse uploadResponse; try { uploadResponse = await UploadValuesAsync <ImgurUploadResponse>(w, "https://api.imgur.com/3/upload.json", values); if (!uploadResponse.Success) { throw new Exception("Response indicates Failure"); } } catch (Exception e) { progressItem.Finished = true; progressItem.Success = false; progressItem.PrimaryText = _loc.ImgurFailed; return(e); } var link = uploadResponse.Data.Link; progressItem.Finished = true; progressItem.Success = true; progressItem.PrimaryText = _loc.ImgurSuccess; progressItem.SecondaryText = link; progressItem.Click += () => Process.Start(link); return(uploadResponse); } }
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)); } }