public void OnAddLocalImage() { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Image Files|*.BMP;*.JPG;*.JPEG,*.PNG"; fileDialog.ShowDialog(); if (fileDialog.CheckFileExists && fileDialog.FileName != "") { var result = MessageBox.Show("Чи це кіт?", "Це кіт?", MessageBoxButton.YesNo, MessageBoxImage.Question); try { byte[] selectImageBytes; using (var selectFileStream = new FileStream(fileDialog.FileName, FileMode.OpenOrCreate)) { selectImageBytes = new byte[selectFileStream.Length]; selectFileStream.Read(selectImageBytes, 0, (int)selectFileStream.Length); } var localImage = LocalImage.GenerateNewLocalFileFromImageTypeAndData(result == MessageBoxResult.Yes ? ImageType.CAT : ImageType.NOT_CAT, selectImageBytes).Result; LocalImageCollection.Insert(0, localImage); } catch { MessageBox.Show("Неможливо відкрити файл - " + fileDialog.FileName, "Неможливо відкрити файл!"); } } }
public void OnConvertRemoteImagesToLocal() { if (RemoteImageCollection.Count == 0) { MessageBox.Show("Немає зображень у віддаленому списку!", "Додайте зображення з CSV файлу"); return; } foreach (var remoteImage in RemoteImageCollection) { remoteImage.Uploaded = false; } this.GeneratingLocalImagesFromRemote = true; var standardStatusMessage = "Перетворення Urls в локальні зображення. Будь ласка, зачекайте... "; this.StatusMessage = standardStatusMessage; var counter = 0; Task task = new Task(() => { Parallel.ForEach(RemoteImageCollection, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (remoteImage) => { remoteImage.UploadInProgress = true; LocalImage localImage; try { localImage = LocalImage.GenerateLocalImageFromRemote(remoteImage).Result; } catch { remoteImage.Failed = true; return; } App.Current.Dispatcher.Invoke(() => { LocalImageCollection.Insert(0, localImage); remoteImage.Uploaded = true; }); remoteImage.UploadInProgress = false; counter++; this.StatusMessage = (standardStatusMessage + (((double)counter / (double)RemoteImageCollection.Count) * 100).ToString("F2") + "%"); }); }); task.Start(); task.ContinueWith(new Action <Task>((task1) => { this.GeneratingLocalImagesFromRemote = false; this.StatusMessage = "Готово"; })); }
public void OnConvertRemoteImagesToLocal() { if (RemoteImageCollection.Count == 0) { MessageBox.Show("No Images in Remote Image List to Download!", "Add Remote Images by CSV File"); return; } foreach (var remoteImage in RemoteImageCollection) { remoteImage.Uploaded = false; } this.GeneratingLocalImagesFromRemote = true; var standardStatusMessage = "Converting Remote Urls into LocalImages. Please Wait... "; this.StatusMessage = standardStatusMessage; var counter = 0; Task task = new Task(() => { Parallel.ForEach(RemoteImageCollection, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (remoteImage) => { remoteImage.UploadInProgress = true; LocalImage localImage; try { localImage = LocalImage.GenerateLocalImageFromRemote(remoteImage).Result; } catch { remoteImage.Failed = true; return; } App.Current.Dispatcher.Invoke(() => { LocalImageCollection.Insert(0, localImage); remoteImage.Uploaded = true; }); remoteImage.UploadInProgress = false; counter++; this.StatusMessage = (standardStatusMessage + (((double)counter / (double)RemoteImageCollection.Count) * 100).ToString("F2") + "%"); }); }); task.Start(); task.ContinueWith(new Action <Task>((task1) => { this.GeneratingLocalImagesFromRemote = false; this.StatusMessage = "Ready"; })); }