private void UploadClipboard(bool _Anonymous) { APIResponses.ImageResponse resp = null; Image clipboardImage = null; string clipboardURL = string.Empty; //bool anonymous = !Properties.Settings.Default.useAccount || !ImgurAPI.HasBeenAuthorized(); bool anonymous = _Anonymous; if (Clipboard.ContainsImage()) { clipboardImage = Clipboard.GetImage(); ShowBalloonTip(4000, "Hold on...", "Attempting to upload image to Imgur...", ToolTipIcon.None); resp = ImgurAPI.UploadImage(clipboardImage, GetTitleString(null), GetDescriptionString(null), _Anonymous); } else if (Clipboard.ContainsText()) { clipboardURL = Clipboard.GetText(TextDataFormat.UnicodeText); Uri uriResult; if (Uri.TryCreate(clipboardURL, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)) { ShowBalloonTip(4000, "Hold on...", "Attempting to upload image to Imgur...", ToolTipIcon.None); resp = ImgurAPI.UploadImage(clipboardURL, GetTitleString(null), GetDescriptionString(null), _Anonymous); } else { ShowBalloonTip(2000, "Can't upload clipboard!", "There's text on the clipboard but it's not a valid URL", ToolTipIcon.Error, true); return; } } else { ShowBalloonTip(2000, "Can't upload clipboard!", "There's no image or URL there", ToolTipIcon.Error, true); return; } if (resp.success) { // this doesn't need an invocation guard because this function can't be called from the context menu if (Properties.Settings.Default.copyLinks) { Clipboard.SetText(resp.data.link); } ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + resp.data.link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.timestamp = DateTime.Now; item.id = resp.data.id; item.link = resp.data.link; item.deletehash = resp.data.deletehash; item.title = resp.data.title; item.description = resp.data.description; item.anonymous = anonymous; if (clipboardImage != null) { item.thumbnail = clipboardImage.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, System.IntPtr.Zero); } History.StoreHistoryItem(item); } else { ShowBalloonTip(2000, "Failed", "Could not upload image (" + resp.status + "):", ToolTipIcon.None, true); } if (!Properties.Settings.Default.clearClipboardOnUpload) { if (clipboardImage != null) { Clipboard.SetImage(clipboardImage); } else { Clipboard.SetText(clipboardURL, TextDataFormat.UnicodeText); } } }
private void UploadFile(bool _Anonymous, string[] _Paths = null) { if (_Paths == null) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true; DialogResult res = dialog.ShowDialog(); if (res == DialogResult.OK) { _Paths = dialog.FileNames; } } if (_Paths != null) { if (_Paths.Length > 1 && Properties.Settings.Default.uploadMultipleImagesAsGallery) { UploadAlbum(_Anonymous, _Paths, ""); return; } int success = 0; int failure = 0; int i = 0; foreach (string fileName in _Paths) { ++i; if (fileName == null || fileName == string.Empty) { continue; } string fileCounterString = (_Paths.Length > 1) ? (" (" + i.ToString() + "/" + _Paths.Length.ToString() + ") ") : (string.Empty); try { ShowBalloonTip(2000, "Hold on..." + fileCounterString, "Attempting to upload image to Imgur...", ToolTipIcon.None); Image img; APIResponses.ImageResponse resp; using (System.IO.FileStream stream = System.IO.File.Open(fileName, System.IO.FileMode.Open)) { // a note to the future: ImgurAPI.UploadImage called Image.Save(); Image.Save() // requires that the stream it was loaded from still be open, or else you get // an immensely generic error. img = System.Drawing.Image.FromStream(stream); FormattingHelper.FormattingContext format_context = new FormattingHelper.FormattingContext(); format_context.m_Filepath = fileName; resp = ImgurAPI.UploadImage(img, GetTitleString(format_context), GetDescriptionString(format_context), _Anonymous); } if (resp.success) { success++; if (Properties.Settings.Default.copyLinks) { // clipboard calls can only be made on an STA thread, threading model is MTA when invoked from context menu if (System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA) { this.Invoke(new Action(delegate { Clipboard.SetText(resp.data.link); })); } else { Clipboard.SetText(resp.data.link); } } ShowBalloonTip(2000, "Success!" + fileCounterString, Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + resp.data.link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.timestamp = DateTime.Now; item.id = resp.data.id; item.link = resp.data.link; item.deletehash = resp.data.deletehash; item.title = resp.data.title; item.description = resp.data.description; item.anonymous = _Anonymous; item.thumbnail = img.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, System.IntPtr.Zero); Invoke(new Action(() => { History.StoreHistoryItem(item); })); } else { failure++; ShowBalloonTip(2000, "Failed" + fileCounterString, "Could not upload image (" + resp.status + "): " + resp.data.error, ToolTipIcon.None, true); } } catch (System.IO.FileNotFoundException) { failure++; ShowBalloonTip(2000, "Failed" + fileCounterString, "Could not find image file on disk (" + fileName + "):", ToolTipIcon.Error, true); } catch (IOException) { ShowBalloonTip(2000, "Failed" + fileCounterString, "Image is in use by another program (" + fileName + "):", ToolTipIcon.Error, true); } } if (_Paths.Length > 1) { ShowBalloonTip(2000, "Done", "Successfully uploaded " + success.ToString() + " files" + ((failure > 0) ? (" (Warning: " + failure.ToString() + " failed)") : (string.Empty)), ToolTipIcon.Info, failure > 0); } } }
private void UploadAlbum(bool _Anonymous, string[] _Paths, string _AlbumTitle) { ShowBalloonTip(2000, "Hold on...", "Attempting to upload album to Imgur (this may take a while)...", ToolTipIcon.None); // Create the album APIResponses.AlbumResponse album_response = ImgurAPI.CreateAlbum(_AlbumTitle, _Anonymous); if (!album_response.Success) { ShowBalloonTip(2000, "Failed", "Could not create album (" + album_response.Status + "): " + album_response.ResponseData.Error.Message, ToolTipIcon.None, true); Statistics.GatherAndSend(); return; } // If we did manage to create the album we can now try to add images to it int succeeded_count = 0; int failed_count = 0; int image_idx = 0; Image album_thumbnail = null; foreach (string path in _Paths) { // Instead of loading every file into memory, peek only their header to check if they are valid images try { Image img = Image.FromStream(new MemoryStream(File.ReadAllBytes(path))); string title = string.Empty; string description = string.Empty; FormattingHelper.FormattingContext format_context = new FormattingHelper.FormattingContext(); format_context.FilePath = path; format_context.AlbumIndex = ++image_idx; APIResponses.ImageResponse resp = ImgurAPI.UploadImage(img, GetTitleString(format_context), GetDescriptionString(format_context), _Anonymous, ref album_response); // If we haven't selected any thumbnail yet, or if the currently uploaded image has been turned into the cover image of the album, turn it into a thumbnail for the history view. if (album_thumbnail == null || resp.ResponseData.Id == album_response.ResponseData.Cover) { album_thumbnail = GenerateThumbnailImage(img, pictureBoxHistoryThumb.Width, pictureBoxHistoryThumb.Height); } if (resp.Success) { succeeded_count++; } else { failed_count++; } } catch (ArgumentException) { ShowBalloonTip(2000, "Failed", "File (" + path + ") is not a valid image file.", ToolTipIcon.Error, true); } catch (FileNotFoundException) { ShowBalloonTip(2000, "Failed", "Could not find image file on disk (" + path + "):", ToolTipIcon.Error, true); } catch (IOException) { ShowBalloonTip(2000, "Failed", "Image is in use by another program (" + path + "):", ToolTipIcon.Error, true); } } // If no images managed to upload to the album we should delete it again if (failed_count == _Paths.Count()) { ShowBalloonTip(2000, "Album upload cancelled", "All images failed to upload! Check the log for more info.", ToolTipIcon.Error, true); // Delete the album because we couldn't upload anything ImgurAPI.DeleteAlbum(album_response.ResponseData.DeleteHash, _Anonymous); Statistics.GatherAndSend(); return; } // Did we succeed in uploading the album? if (album_response.Success) { // Clipboard calls can only be made on an STA thread, threading model is MTA when invoked from context menu if (System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA) { this.Invoke(new Action(() => Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks ? album_response.ResponseData.Link.Replace("http://", "https://") : album_response.ResponseData.Link))); } else { Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks ? album_response.ResponseData.Link.Replace("http://", "https://") : album_response.ResponseData.Link); } ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + album_response.ResponseData.Link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.Timestamp = DateTime.Now; item.Id = album_response.ResponseData.Id; item.Link = album_response.ResponseData.Link; item.Deletehash = album_response.ResponseData.DeleteHash; item.Title = album_response.ResponseData.Title; item.Description = album_response.ResponseData.Description; item.Anonymous = _Anonymous; item.Album = true; item.Thumbnail = album_thumbnail; Invoke(new Action(() => History.StoreHistoryItem(item))); } else { ShowBalloonTip(2000, "Failed", "Could not upload album (" + album_response.Status + "): " + album_response.ResponseData.Error.Message, ToolTipIcon.None, true); } Statistics.GatherAndSend(); }