Exemple #1
0
        private async Task SaveAllImages(List <string> imageList)
        {
            //Have user set folderBrowserDialog1.SelectedPath property
            folderBrowserDialog1.SelectedPath = string.Empty;
            folderBrowserDialog1.ShowDialog();
            string path = folderBrowserDialog1.SelectedPath;

            if (path == string.Empty)
            {
                return;
            }

            FormatListItems(imageList);

            CallingMethodsAsync    caller    = new CallingMethodsAsync();
            ImageValidationMethods validator = new ImageValidationMethods();

            for (int i = 0; i < imageList.Count; i++)
            {
                try
                {
                    byteList.Add(await caller.DownloadImageAsync(imageList[i]));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Warning",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                label1.Text = string.Format("Downloaded {0} of {1} images.", byteList.Count, imageList.Count);
            }

            int counter = 0;

            for (int i = 0; i < byteList.Count; i++)
            {
                if (byteList[i].Length > 1)
                {
                    if (validator.IsValidImageFormat(byteList[i]))
                    {
                        fileExtension = validator.ReturnImageExtension(byteList[i]);
                        try
                        {
                            await File.WriteAllBytesAsync(Path.Combine(path, "image" + i + fileExtension), byteList[i]);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    else
                    {
                        counter++;
                    }
                }
                else
                {
                    counter++;
                }
            }

            if (counter != 0)
            {
                MessageBox.Show(counter == 1 ? $"{counter} image could not be saved" :
                                $"{counter} images could not be saved",
                                "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }