/// <summary> /// Loads image at specified index /// </summary> /// <param name="index">The index of file to load from Pics</param> internal static async Task LoadPicAt(int index) { Preloader.PreloadValue preloadValue; // Error checking to fix rare cases of crashing if (Pics.Count < index) { preloadValue = await PicErrorFix(index).ConfigureAwait(true); if (preloadValue == null) { /// Try to recover /// TODO needs testing Reload(true); return; } } FolderIndex = index; preloadValue = Preloader.Get(Pics[index]); // Initate loading behavior, if needed if (preloadValue == null || preloadValue.isLoading) { CanNavigate = false; // Dissallow changing image while loading if (!GalleryFunctions.IsOpen) { // Show a thumbnail while loading var thumb = GetThumb(index); await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() => { // Set loading from translation service SetLoadingString(); // Don't allow image size to stretch the whole screen if (xWidth == 0) { ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth; ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight; } if (thumb != null) { ConfigureWindows.GetMainWindow.MainImage.Source = thumb; } })); } if (preloadValue == null) // Error correctiom { await Preloader.Add(Pics[index]).ConfigureAwait(true); preloadValue = Preloader.Get(Pics[index]); } while (preloadValue.isLoading) { // Wait for finnished result await Task.Delay(3).ConfigureAwait(true); } } // Check if works, if not show error message if (preloadValue == null || preloadValue.bitmapSource == null) { preloadValue = new Preloader.PreloadValue(ImageDecoder.ImageErrorMessage(), false); } // Need to put UI change in dispatcher to fix slideshow bug await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, (Action)(() => { // Scroll to top if scroll enabled if (IsScrollEnabled) { ConfigureWindows.GetMainWindow.Scroller.ScrollToTop(); } // Reset transforms if needed if (UILogic.TransformImage.Rotation.Flipped || UILogic.TransformImage.Rotation.Rotateint != 0) { UILogic.TransformImage.Rotation.Flipped = false; UILogic.TransformImage.Rotation.Rotateint = 0; GetImageSettingsMenu.FlipButton.TheButton.IsChecked = false; ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null; } ConfigureWindows.GetMainWindow.MainImage.Source = preloadValue.bitmapSource; FitImage(preloadValue.bitmapSource.PixelWidth, preloadValue.bitmapSource.PixelHeight); SetTitleString(preloadValue.bitmapSource.PixelWidth, preloadValue.bitmapSource.PixelHeight, index); })); // Update values CanNavigate = true; FreshStartup = false; if (ConfigureWindows.GetImageInfoWindow != null) { if (ConfigureWindows.GetImageInfoWindow.IsVisible) { ConfigureWindows.GetImageInfoWindow.UpdateValues(); } } if (Pics.Count > 1) { Taskbar.Progress(index, Pics.Count); // Preload images \\ await Preloader.PreLoad(index).ConfigureAwait(false); } // Add recent files, except when browing archive if (string.IsNullOrWhiteSpace(TempZipFile)) { RecentFiles.Add(Pics[index]); } }
/// <summary> /// Attemps to fix erros and prevent crashes /// </summary> /// <param name="x">The index to start from</param> internal static async Task <Preloader.PreloadValue> PicErrorFix(int x) { Preloader.PreloadValue pic; #if DEBUG Trace.WriteLine("Entered PicErrorFix"); #endif if (Pics == null) { Reload(true); return(null); } if (x == -1) { await GetValues(Pics[0]).ConfigureAwait(true); } if (Pics.Count < 0) { ShowTooltipMessage(Application.Current.Resources["UnexpectedError"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } else if (x >= Pics.Count) { if (Pics.Count > 0) { if (x < Pics.Count) { pic = new Preloader.PreloadValue(await RenderToBitmapSource(Pics[x]).ConfigureAwait(true), false); if (pic != null) { return(pic); } } } else { Unload(); return(null); } } else if (x < 0) { pic = new Preloader.PreloadValue(await RenderToBitmapSource(Pics[x]).ConfigureAwait(true), false); if (pic != null) { return(pic); } else { Pics = FileList(Path.GetDirectoryName(Pics[x])); Pics.Remove(Pics[x]); x--; if (x < 0) { Unload(); return(null); } } } var file = Pics[x]; if (file == null) { ShowTooltipMessage(Application.Current.Resources["UnexpectedError"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Retry if exists, fixes rare error if (File.Exists(file)) { pic = new Preloader.PreloadValue(await RenderToBitmapSource(file).ConfigureAwait(true), false); if (pic != null) { return(pic); } return(null); } // Continue to remove file if can't be rendered Pics.Remove(file); // Check if there's still images in folder if (Pics.Count < 0) { ShowTooltipMessage(Application.Current.Resources["No images"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Go to next image if (Properties.Settings.Default.Looping) { x = x == Pics.Count - 1 ? 0 : FolderIndex; } else { x = x == Pics.Count - 1 ? Pics.Count - 2 : FolderIndex; } // Repeat process if the next image was not found if (x > 0 && x < Pics.Count) { await PicErrorFix(x).ConfigureAwait(false); } return(null); }