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); List <Image> images = new List <Image>(); List <string> titles = new List <string>(); List <string> descriptions = new List <string>(); int i = 0; foreach (string path in _Paths) { try { images.Add(Image.FromStream(new MemoryStream(File.ReadAllBytes(path)))); //ìmages.Add(System.Drawing.Image.FromStream(stream)); string title = string.Empty; string description = string.Empty; FormattingHelper.FormattingContext format_context = new FormattingHelper.FormattingContext(); format_context.m_Filepath = path; format_context.m_AlbumIndex = ++i; titles.Add(GetTitleString(format_context)); descriptions.Add(GetDescriptionString(format_context)); } 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); } } APIResponses.AlbumResponse response = ImgurAPI.UploadAlbum(images.ToArray(), _AlbumTitle, _Anonymous, titles.ToArray(), descriptions.ToArray()); if (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(delegate { Clipboard.SetText(response.data.link); })); } else { Clipboard.SetText(response.data.link); } ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + response.data.link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.timestamp = DateTime.Now; item.id = response.data.id; item.link = response.data.link; item.deletehash = response.data.deletehash; item.title = response.data.title; item.description = response.data.description; item.anonymous = _Anonymous; item.album = true; item.thumbnail = response.CoverImage.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, System.IntPtr.Zero); Invoke(new Action(() => { History.StoreHistoryItem(item); })); } else { ShowBalloonTip(2000, "Failed", "Could not upload album (" + response.status + "): " + response.data.error, ToolTipIcon.None, true); } }
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 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.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(() => Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks ? resp.ResponseData.Link.Replace("http://", "https://") : resp.ResponseData.Link))); } else { Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks ? resp.ResponseData.Link.Replace("http://", "https://") : resp.ResponseData.Link); } } ShowBalloonTip(2000, "Success!" + fileCounterString, Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + resp.ResponseData.Link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.Timestamp = DateTime.Now; item.Id = resp.ResponseData.Id; item.Link = resp.ResponseData.Link; item.Deletehash = resp.ResponseData.DeleteHash; item.Title = resp.ResponseData.Title; item.Description = resp.ResponseData.Description; item.Anonymous = _Anonymous; item.Thumbnail = img.GetThumbnailImage(pictureBoxHistoryThumb.Width, pictureBoxHistoryThumb.Height, null, System.IntPtr.Zero); Invoke(new Action(() => History.StoreHistoryItem(item))); } else { failure++; ShowBalloonTip(2000, "Failed" + fileCounterString, "Could not upload image (" + resp.Status + "): " + resp.ResponseData.Error, ToolTipIcon.None, true); } } catch (ArgumentException) { ShowBalloonTip(2000, "Failed" + fileCounterString, "File (" + fileName + ") is not a valid image file.", ToolTipIcon.Error, 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); } } Statistics.GatherAndSend(); }
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 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(Properties.Settings.Default.copyHttpsLinks ? resp.ResponseData.Link.Replace("http://", "https://") : resp.ResponseData.Link); } ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + resp.ResponseData.Link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.Timestamp = DateTime.Now; item.Id = resp.ResponseData.Id; item.Link = resp.ResponseData.Link; item.Deletehash = resp.ResponseData.DeleteHash; item.Title = resp.ResponseData.Title; item.Description = resp.ResponseData.Description; item.Anonymous = anonymous; if (clipboardImage != null) item.Thumbnail = clipboardImage.GetThumbnailImage(pictureBoxHistoryThumb.Width, pictureBoxHistoryThumb.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); } Statistics.GatherAndSend(); }
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); List<Image> images = new List<Image>(); List<string> titles = new List<string>(); List<string> descriptions = new List<string>(); int i = 0; foreach (string path in _Paths) { try { images.Add(Image.FromStream(new MemoryStream(File.ReadAllBytes(path)))); //ìmages.Add(System.Drawing.Image.FromStream(stream)); string title = string.Empty; string description = string.Empty; FormattingHelper.FormattingContext format_context = new FormattingHelper.FormattingContext(); format_context.FilePath = path; format_context.AlbumIndex = ++i; titles.Add(GetTitleString(format_context)); descriptions.Add(GetDescriptionString(format_context)); } 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 (images.Count == 0) { Log.Error("Album upload failed: No valid images in selected images!"); ShowBalloonTip(2000, "Failed", "Album upload cancelled: No valid images to upload!", ToolTipIcon.Error, true); return; } APIResponses.AlbumResponse response = ImgurAPI.UploadAlbum(images.ToArray(), _AlbumTitle, _Anonymous, titles.ToArray(), descriptions.ToArray()); if (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 ? response.ResponseData.Link.Replace("http://", "https://") : response.ResponseData.Link))); } else { Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks ? response.ResponseData.Link.Replace("http://", "https://") : response.ResponseData.Link); } ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + response.ResponseData.Link, ToolTipIcon.None); HistoryItem item = new HistoryItem(); item.Timestamp = DateTime.Now; item.Id = response.ResponseData.Id; item.Link = response.ResponseData.Link; item.Deletehash = response.ResponseData.DeleteHash; item.Title = response.ResponseData.Title; item.Description = response.ResponseData.Description; item.Anonymous = _Anonymous; item.Album = true; item.Thumbnail = response.CoverImage.GetThumbnailImage(pictureBoxHistoryThumb.Width, pictureBoxHistoryThumb.Height, null, System.IntPtr.Zero); Invoke(new Action(() => History.StoreHistoryItem(item))); } else ShowBalloonTip(2000, "Failed", "Could not upload album (" + response.Status + "): " + response.ResponseData.Error, ToolTipIcon.None, true); Statistics.GatherAndSend(); }
public static void RemoveHistoryItem(HistoryItem item) { // This was changed because BindingSource doesn't support RemoveAll. if(!_historyBinding.Contains(item)) { Log.Warning("Failed to remove history item from list. Item is not present in list."); return; } _historyBinding.Remove(item); StoreHistoryOnDisk(); HistoryItemRemoved(item); }
public static void StoreHistoryItem(HistoryItem item) { if (item == null) { Log.Warning("NULL object passed to History.StoreHistoryItem. No item stored."); return; } if(_historyBinding.Contains(item)) { Log.Warning("Object already in history passed to History.StoreHistoryItem. No item stored."); return; } _historyBinding.Add(item); HistoryItemAdded(item); StoreHistoryOnDisk(); }
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(); }