private void btnSaveCurrent_ClickAsync(object sender, EventArgs e) { try { int width = Convert.ToInt32(txtNewSize.Text); if (width >= 1 && width <= 2000) { if (imagesListBox.SelectedItem != null) { FlickrResult selectedImage = (FlickrResult)imagesListBox.SelectedItem; ResizeImage(selectedImage, width); MessageBox.Show("The image is saved"); } } else { MessageBox.Show("Size of an image should be in range 1 to 2000"); } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
// ------------------Added Code------------------ // All images are saved in FlikrViewer/bin/Debug/Images private static async void ResizeImage(FlickrResult photo, int width) { await Task.Factory.StartNew(() => { try { var webClient = new WebClient(); byte[] imageBytes = webClient.DownloadData(photo.URL); using (var stream = new MemoryStream(imageBytes)) { var image = Image.FromStream(stream); var height = (width *image.Height) / image.Width; var thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero); if (photo.Title != "" && photo.Title != null && photo.Title.Count() <= 250) { thumbnail.Save(@"Images\" + photo.Title + ".jpeg"); } } } catch { } }); }