/// <summary> /// Loads image at specified index /// </summary> /// <param name="index">The index of file to load from Pics</param> internal static async void Pic(int index) { #if DEBUG var stopWatch = new Stopwatch(); stopWatch.Start(); #endif // Declare variable to be used to set image source BitmapSource bitmapSource; // Error checking to fix rare cases of crashing if (Pics.Count < index) { bitmapSource = await PicErrorFix(index).ConfigureAwait(true); if (bitmapSource == null) { /// Try to recover /// TODO needs testing Reload(true); return; } } else if (File.Exists(Pics[index])) // Checking if file exists fixes rare crashes { /// Use the Load() function load image from memory if available /// if not, it will be null bitmapSource = Preloader.Load(Pics[index]); } else { /// Try to reload from backup if file does not exist /// TODO needs testing Reload(true); return; } // Initate loading behavior, if needed if (bitmapSource == null) { // Set loading from translation service SetLoadingString(); // Show a thumbnail while loading var thumb = GetThumb(index, true); if (thumb != null && Properties.Settings.Default.PicGallery != 2) { // Don't allow image size to stretch the whole screen if (xWidth == 0) { var size = ImageDecoder.ImageSize(Pics[index]); if (size.HasValue) { FitImage(size.Value.Width, size.Value.Height); } else { LoadWindows.GetMainWindow.MainImage.Width = LoadWindows.GetMainWindow.MinWidth; LoadWindows.GetMainWindow.MainImage.Height = LoadWindows.GetMainWindow.MinHeight; } } else { LoadWindows.GetMainWindow.MainImage.Width = xWidth; LoadWindows.GetMainWindow.MainImage.Height = xHeight; } LoadWindows.GetMainWindow.MainImage.Source = thumb; } // Dissallow changing image while loading CanNavigate = false; // Get it! await Preloader.Add(Pics[index]).ConfigureAwait(true); // Retry bitmapSource = Preloader.Load(Pics[index]); if (bitmapSource == null) { // Attempt to fix it bitmapSource = await PicErrorFix(index).ConfigureAwait(true); // If pic is still null, image can't be rendered if (bitmapSource == null) { // Clean up Pics.RemoveAt(index); Preloader.Remove(index); // Sync with gallery, if needed if (GetPicGallery != null) { if (GetPicGallery.grid.Children.Count > index) { GetPicGallery.grid.Children.RemoveAt(index); } } // Check if images still exists if (Pics.Count == 0) { Unload(); return; } /// Retry /// TODO needs testing CanNavigate = true; Pic(); return; } } } // 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; LoadWindows.GetMainWindow.MainImage.LayoutTransform = null; } // Show the image! :) LoadWindows.GetMainWindow.MainImage.Source = bitmapSource; FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight); SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index); // Scroll to top if scroll enabled if (IsScrollEnabled) { LoadWindows.GetMainWindow.Scroller.ScrollToTop(); } // Update values CanNavigate = true; FolderIndex = index; FreshStartup = false; if (LoadWindows.GetImageInfoWindow != null) { if (LoadWindows.GetImageInfoWindow.IsVisible) { LoadWindows.GetImageInfoWindow.UpdateValues(); } } if (Pics.Count > 1) { Taskbar.Progress(index, Pics.Count); // Preload images \\ await Preloader.PreLoad(index).ConfigureAwait(false); } RecentFiles.Add(Pics[index]); #if DEBUG stopWatch.Stop(); var s = $"Pic(); executed in {stopWatch.Elapsed.TotalMilliseconds} milliseconds"; Trace.WriteLine(s); #endif }