/// <summary> /// Upload a single File to Imgur /// </summary> /// <param name="file">Path to Image</param> public static async Task UploadSingle(string file) { ImgurUploader imgur = await GetUploaderAsync(); using (MemoryStream stream = new MemoryStream()) { try { //Binary Image using (FileStream fstream = new FileStream(file, FileMode.Open, FileAccess.Read)) { await fstream.CopyToAsync(stream); //Set Stream Position to 0 stream.Position = 0; } //Image Size string kb = $"{stream.Length / 1024d:0.#}"; //e.g. "Uploading Image (123KB)" ShowNotification(string.Format(strings.uploading, kb), NotificationType.Progress, false); string link = await imgur.Upload(stream); await ClipboardHelper.CopyLink(link, false); Notification?.Close(); } catch { //Unsupported File Type? Internet connection error? await ShowNotificationAsync(strings.errorInstantUpload, NotificationType.Error, ActionTroubleshoot); } } await Task.Delay(500); Application.Current.Shutdown(0); }
public static void SendImage(Buddy buddy, Image img, Action <Buddy, ImgurResponse> callBack) { using (BackgroundWorker backgroundWorker = new BackgroundWorker()) { backgroundWorker.RunWorkerCompleted += (sender, e) => callBack(buddy, e.Result as ImgurResponse); backgroundWorker.DoWork += (sender, e) => { e.Result = ImgurUploader.PostToImgur(img); }; backgroundWorker.RunWorkerAsync(); } }
public async static Task <ImgurUploader> GetUploaderAsync() { if (_client == null) { _client = new ImgurUploader(); } await _client.Login(); // Will not do anything if token is still valid return(_client); }
static void Main(string[] args) { var uploader = new ImgurUploader(); var result = uploader.Upload("fb39e7076e0e8cd", new ImageData(File.ReadAllBytes(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")) { Title = "Testing", Description = "A Testing DESC" }); Console.WriteLine(result); Console.ReadLine(); }
/// <summary> /// Upload the image to the specified host /// </summary> /// <param name="img"></param> public static void Upload(System.Drawing.Bitmap img) { switch (Config.conf.Uploader.ToLower()) { default: case "imgur": ImgurUploader.Upload(Image.bitmapToBase64(img)); break; case "vgyme": VgyMeUploader.Upload(img); break; case "gyazo": GyazoUploader.Upload(img); break; } }
/// <summary> /// Upload more than one Image to Imgur Album /// </summary> /// <param name="files">Path to all upload-queued Images</param> public static async Task UploadMultiple(IEnumerable <string> files) { try { ImgurUploader imgur = await GetUploaderAsync(); //Binary Image List <MemoryStream> images = new List <MemoryStream>(); //Load every Image double size = 0; Parallel.ForEach(files, async(f) => { using (FileStream fstream = new FileStream(f, FileMode.Open, FileAccess.Read)) { //Init MemoryStream with FileStream Contents MemoryStream mstream = new MemoryStream(); await fstream.CopyToAsync(mstream); //Set Position to 0 and add to List mstream.Position = 0; images.Add(mstream); size += mstream.Length; } }); //Image Size string kb = $"{size / 1024d:0.#}"; //Key = Album ID | Value = Album Delete Hash (Key = Value if User is logged in) Tuple <string, string> albumInfo = await imgur.CreateAlbum(); ShowNotification("", NotificationType.Progress, false); int index = 1; //Upload each image foreach (MemoryStream image in images) { using (image) { try { Notification.ContentLabel.Text = string.Format(strings.uploadingFiles, kb, index, images.Count); await imgur.UploadToAlbum(image, "", albumInfo.Item2); } catch (Exception e) { Console.WriteLine(e.Message); //this image was not uploaded } finally { index++; } } //Dispose image } Notification?.Close(); await OpenAlbum(albumInfo.Item1); } catch { //Unsupported File Type? Internet connection error? await ShowNotificationAsync(strings.errorInstantUpload, NotificationType.Error, ActionTroubleshoot); } await Task.Delay(500); Application.Current.Shutdown(0); }
public async static Task<string> ToImgur(Image img) { var uploader = new ImgurUploader(img); return await uploader.UploadAsync(); }