Ruben 4 лет назад
Родитель
Сommit
09d857d848

+ 2 - 2
PicView/ChangeImage/Error_Handling.cs

@@ -113,11 +113,11 @@ namespace PicView.ChangeImage
                 if (containerCheck)
                 {
                     await GalleryFunctions.SortGallery().ConfigureAwait(false);
-                    await LoadPicAtIndexAsync(FolderIndex).ConfigureAwait(false);
+                    await LoadPic.LoadPicAtIndexAsync(FolderIndex).ConfigureAwait(false);
                 }
                 else
                 {
-                    await LoadPiFromFileAsync(path).ConfigureAwait(false);
+                    await LoadPic.LoadPiFromFileAsync(path).ConfigureAwait(false);
                 }
 
                 // Reset

+ 718 - 0
PicView/ChangeImage/LoadPic.cs

@@ -0,0 +1,718 @@
+using PicView.FileHandling;
+using PicView.ImageHandling;
+using PicView.PicGallery;
+using PicView.SystemIntegration;
+using PicView.UILogic;
+using System;
+using System.Globalization;
+using System.IO;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using static PicView.ChangeImage.Error_Handling;
+using static PicView.ChangeImage.Navigation;
+using static PicView.FileHandling.ArchiveExtraction;
+using static PicView.FileHandling.FileLists;
+using static PicView.ImageHandling.Thumbnails;
+using static PicView.UILogic.SetTitle;
+using static PicView.UILogic.Sizing.ScaleImage;
+using static PicView.UILogic.Tooltip;
+using static PicView.UILogic.UC;
+
+namespace PicView.ChangeImage
+{
+    internal static class LoadPic
+    {
+        /// <summary>
+        /// Quickly load image and then update values
+        /// Only to be used from startup
+        /// </summary>
+        /// <param name="file"></param>
+        /// <returns></returns>
+        internal static async Task QuickLoad(string file)
+        {
+            if (File.Exists(file) == false)
+            {
+                await LoadPicFromString(file, false).ConfigureAwait(false);
+                return;
+            }
+
+            bool archive = false;
+            var pic = await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
+            if (pic is null)
+            {
+                archive = SupportedFiles.IsSupportedArchives(file);
+                if (archive == false)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Unload();
+                    });
+                    return;
+                }
+            }
+            else if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.Width;
+                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.Height;
+                });
+            }
+
+            await GetValues(file).ConfigureAwait(false);
+
+            switch (Pics.Count)
+            {
+                case > 0:
+                    FolderIndex = Pics.IndexOf(file);
+                    break;
+                default:
+                    FolderIndex = 0;
+                    break;
+            }
+
+            if (archive == false)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+                {
+                    UpdatePic(FolderIndex, pic);
+                    if (Properties.Settings.Default.AutoFitWindow)
+                    {
+                        UILogic.Sizing.WindowSizing.SetWindowBehavior();
+                    }
+                });
+
+                await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+            }
+
+            if (FolderIndex > 0)
+            {
+                await Taskbar.Progress((double)FolderIndex / Pics.Count).ConfigureAwait(false);
+            }
+
+            if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
+            {
+                await GalleryLoad.Load().ConfigureAwait(false);
+            }
+
+            FreshStartup = false;
+
+            // Add recent files, except when browing archive
+            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > FolderIndex)
+            {
+                RecentFiles.Add(Pics?[FolderIndex]);
+            }
+        }
+
+        /// <summary>
+        /// Determine proper path from given string value
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        internal static async Task LoadPicFromString(string path, bool checkExists = true)
+        {
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+            {
+                // Set Loading
+                SetLoadingString();
+
+                if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
+                {
+                    BitmapSource? bitmapSource = GetBitmapSourceThumb(path);
+                    if (bitmapSource != null)
+                    {
+                        ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+                    }
+                }
+
+                // Don't allow image size to stretch the whole screen, fixes when opening new image from unloaded status
+                if (XWidth < 1)
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
+                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
+                }
+            });
+
+            if (checkExists && File.Exists(path))
+            {
+                // set up size so it feels better when starting application
+                await TryFitImageAsync(path).ConfigureAwait(false);
+
+                await LoadPiFromFileAsync(path).ConfigureAwait(false);
+
+                FreshStartup = false;
+            }
+            else
+            {
+                bool result = Uri.TryCreate(path, UriKind.Absolute, out Uri? uriResult)
+                    && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
+
+                if (result)
+                {
+                    await WebFunctions.PicWeb(path).ConfigureAwait(false);
+                    return;
+                }
+                else if (Base64.IsBase64String(path))
+                {
+                    await Pic64(path).ConfigureAwait(false);
+                    return;
+                }
+
+                if (FileFunctions.FilePathHasInvalidChars(path))
+                {
+                    FileFunctions.MakeValidFileName(path);
+                }
+
+                path = path.Replace("\"", "");
+                path = path.Trim();
+
+                if (File.Exists(path))
+                {
+                    await TryFitImageAsync(path).ConfigureAwait(false);
+
+                    await LoadPiFromFileAsync(path).ConfigureAwait(false);
+
+                    FreshStartup = false;
+                }
+
+                else if (Directory.Exists(path))
+                {
+                    await LoadPicFromFolderAsync(path).ConfigureAwait(false);
+
+                    FreshStartup = false;
+                }
+                else
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
+                    {
+                        Unload();
+                    });
+                }
+            }
+        }
+
+        /// <summary>
+        /// Loads a picture from a given file path and does extra error checking
+        /// </summary>
+        /// <param name="path"></param>
+        internal static async Task LoadPiFromFileAsync(string path)
+        {
+            bool folderChanged = false;
+
+            // If count not correct or just started, get values
+            if (Pics?.Count <= FolderIndex || FolderIndex < 0 || FreshStartup)
+            {
+                await GetValues(path).ConfigureAwait(false);
+            }
+            // If the file is in the same folder, navigate to it. If not, start manual loading procedure.
+            else if (!string.IsNullOrWhiteSpace(Pics?[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex]))
+            {
+                // Reset old values and get new
+                ChangeFolder(true);
+                await GetValues(path).ConfigureAwait(false);
+                folderChanged = true;
+            }
+            else if (Pics.Contains(path) == false)
+            {
+                await GetValues(path).ConfigureAwait(false);
+            }
+
+            if (Pics?.Count > 0)
+            {
+                FolderIndex = Pics.IndexOf(path);
+            }
+
+            if (!FreshStartup)
+            {
+                Preloader.Clear();
+            }
+
+            if (FolderIndex >= 0 && Pics?.Count > 0) // check if being extracted and need to wait for it instead
+            {
+                // Navigate to picture using obtained index
+                await LoadPicAtIndexAsync(FolderIndex).ConfigureAwait(false);
+            }
+
+            if (GetPicGallery is not null && folderChanged)
+            {
+                if (Properties.Settings.Default.FullscreenGalleryHorizontal || Properties.Settings.Default.FullscreenGalleryVertical)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() =>
+                    {
+                        if (GetPicGallery == null)
+                        {
+                            return;
+                        }
+
+                        // Remove children before loading new
+                        if (GetPicGallery.Container.Children.Count > 0)
+                        {
+                            GetPicGallery.Container.Children.Clear();
+                        }
+                    }));
+
+                    // Load new gallery values, if changing folder
+                    await GalleryLoad.Load().ConfigureAwait(false);
+                }
+            }
+
+            FreshStartup = false;
+        }
+
+        /// <summary>
+        /// Loads image at specified index
+        /// </summary>
+        /// <param name="index">The index of file to load from Pics</param>
+        internal static async Task LoadPicAtIndexAsync(int index, bool resize = true)
+        {
+            if (Pics?.Count < index || Pics?.Count < 1)
+            {
+                return;
+            }
+
+            if (GetToolTipMessage is not null && GetToolTipMessage.IsVisible)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    GetToolTipMessage.Visibility = Visibility.Hidden;
+                });
+            }
+
+            FolderIndex = index;
+            var preloadValue = Preloader.Get(Navigation.Pics[index]);
+
+            // Initate loading behavior, if needed
+            if (preloadValue == null || preloadValue.isLoading)
+            {
+                // Show a thumbnail while loading
+                BitmapSource? thumb = null;
+
+                if (GalleryFunctions.IsHorizontalFullscreenOpen == false || GalleryFunctions.IsVerticalFullscreenOpen == false)
+                {
+                    thumb = GetBitmapSourceThumb(Pics[FolderIndex]);
+                }
+
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+                    {
+                        thumb = GetThumb(index);
+                    }
+
+                    if (FreshStartup)
+                    {
+                        // Set loading from translation service
+                        SetLoadingString();
+                        FreshStartup = false;
+                    }
+                    else
+                    {
+                        var image = Application.Current.Resources["Image"] as string;
+
+                        ConfigureWindows.GetMainWindow.TitleText.ToolTip =
+                        ConfigureWindows.GetMainWindow.Title =
+                        ConfigureWindows.GetMainWindow.TitleText.Text
+                        = $"{image} {index + 1} / {Pics?.Count}";
+                    }
+
+                    if (thumb != null)
+                    {
+                        ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
+                    }
+                });
+
+                if (FastPicRunning) // Holding down button is too fast and will be laggy when not just loading thumbnails
+                {
+                    return;
+                }
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+
+                    // Make loading skippable
+                    if (FolderIndex != index)
+                    {
+                        // Start preloading when browsing very fast to catch up
+                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                        return;
+                    }
+                }
+                if (preloadValue == null) // Error correctiom
+                {
+                    await Preloader.AddAsync(index).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[index]);
+                }
+            }
+
+            // Make loading skippable
+            if (FolderIndex != index)
+            {
+                // Start preloading when browsing very fast to catch up
+                if (Preloader.IsRunning == false)
+                {
+                    await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                }
+
+                if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        GalleryNavigation.FullscreenGalleryNavigation();
+                    });
+                }
+                return;
+            }
+
+            // Check if works, if not show error message
+            if (preloadValue is not null)
+            {
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+
+                    // Make loading skippable
+                    if (FolderIndex != index)
+                    {
+                        // Start preloading when browsing very fast to catch up
+                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                        return;
+                    }
+                }
+                if (preloadValue is null) // Error correctiom
+                {
+                    await Preloader.AddAsync(index).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[index]);
+                }
+                if (preloadValue is null)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Error_Handling.Unload();
+                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                    });
+                    return;
+                }
+                if (preloadValue.bitmapSource == null)
+                {
+                    preloadValue = new Preloader.PreloadValue(ImageFunctions.ImageErrorMessage(), false);
+
+                    if (preloadValue == null || preloadValue.bitmapSource == null)
+                    {
+                        await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                        {
+                            Error_Handling.Unload();
+                            ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                        });
+                        return;
+                    }
+                }
+            }
+            else
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    Error_Handling.Unload();
+                    ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                });
+                return;
+            }
+
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                UpdatePic(index, preloadValue.bitmapSource, resize);
+            });
+
+            // Update PicGallery selected item, if needed
+            if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    GalleryNavigation.FullscreenGalleryNavigation();
+                });
+            }
+
+            await ImageInfo.UpdateValuesAsync(Pics?[FolderIndex]).ConfigureAwait(false);
+
+            if (Pics?.Count > 1)
+            {
+                if (Preloader.IsRunning == false)
+                {
+                    await Task.Run(() => Preloader.PreLoad(index)).ConfigureAwait(false);
+                }
+
+                if (FolderIndex == index)
+                {
+                    await Taskbar.Progress((double)index / Pics.Count).ConfigureAwait(false);
+                }
+            }
+
+            // Add recent files, except when browing archive
+            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > index)
+            {
+                RecentFiles.Add(Pics?[index]);
+            }
+        }
+
+        /// <summary>
+        /// Update picture, size it and set the title from index
+        /// </summary>
+        /// <param name="index"></param>
+        /// <param name="bitmapSource"></param>
+        internal static void UpdatePic(int index, BitmapSource bitmapSource, bool resise = true)
+        {
+            // Scroll to top if scroll enabled
+            if (Properties.Settings.Default.ScrollEnabled)
+            {
+                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;
+                if (GetQuickSettingsMenu is not null && GetQuickSettingsMenu.FlipButton is not null)
+                {
+                    GetQuickSettingsMenu.FlipButton.TheButton.IsChecked = false;
+                }
+
+                ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
+            }
+
+            // Loads gif from XamlAnimatedGif if neccesary
+            string? ext = Path.GetExtension(Pics?[index]);
+            if (ext is not null && ext.Equals(".gif", StringComparison.OrdinalIgnoreCase))
+            {
+                XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(Pics?[index]));
+            }
+            else
+            {
+                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+            }
+
+            if (resise)
+            {
+                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+            }
+
+            SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index);
+        }
+
+        /// <summary>
+        /// Update picture, size it and set the title from string
+        /// </summary>
+        /// <param name="imageName"></param>
+        /// <param name="bitmapSource"></param>
+        internal static async Task UpdatePic(string imageName, BitmapSource bitmapSource)
+        {
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                Unload();
+
+                if (Properties.Settings.Default.ScrollEnabled)
+                {
+                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
+                }
+
+                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+
+                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+                SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
+
+                CloseToolTipMessage();
+            });
+
+            await Taskbar.NoProgress().ConfigureAwait(false);
+            FolderIndex = 0;
+
+            await ImageInfo.UpdateValuesAsync(imageName).ConfigureAwait(false);
+        }
+
+        /// <summary>
+        /// Load a picture from a prepared bitmap
+        /// </summary>
+        /// <param name="pic"></param>
+        /// <param name="imageName"></param>
+        internal static void Pic(BitmapSource bitmap, string imageName)
+        {
+            _ = UpdatePic(imageName, bitmap);
+
+            DeleteFiles.DeleteTempFiles();
+        }
+
+        /// <summary>
+        /// Load a picture from a prepared string
+        /// </summary>
+        /// <param name="pic"></param>
+        /// <param name="imageName"></param>
+        internal static async Task PicAsync(string file, string imageName, bool isGif)
+        {
+            BitmapSource? bitmapSource = isGif ? null : await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, async () =>
+            {
+                if (Properties.Settings.Default.ScrollEnabled)
+                {
+                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
+                }
+
+                if (isGif)
+                {
+                    Size? imageSize = await ImageFunctions.ImageSizeAsync(file).ConfigureAwait(true);
+                    if (imageSize.HasValue)
+                    {
+                        FitImage(imageSize.Value.Width, imageSize.Value.Height);
+                        SetTitleString((int)imageSize.Value.Width, (int)imageSize.Value.Height, imageName);
+                    }
+                    XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(file));
+                }
+                else if (bitmapSource != null)
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+                    SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
+                    FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+                }
+                else
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Error_Handling.Unload();
+                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                    });
+                    return;
+                }
+
+                CloseToolTipMessage();
+            });
+
+            await Taskbar.NoProgress().ConfigureAwait(false);
+            FolderIndex = 0;
+
+            await ImageInfo.UpdateValuesAsync(file).ConfigureAwait(false);
+
+            DeleteFiles.DeleteTempFiles();
+        }
+
+        /// <summary>
+        /// Load a picture from a base64
+        /// </summary>
+        /// <param name="pic"></param>
+        /// <param name="imageName"></param>
+        internal static async Task Pic64(string base64string)
+        {
+            if (string.IsNullOrEmpty(base64string))
+            {
+                return;
+            }
+            var pic = await Base64.Base64StringToBitmap(base64string).ConfigureAwait(false);
+            if (pic == null)
+            {
+                return;
+            }
+            if (Application.Current.Resources["Base64Image"] is not string b64)
+            {
+                return;
+            }
+            await UpdatePic(b64, pic).ConfigureAwait(false);
+        }
+
+        /// <summary>
+        /// Handle logic if user wants to load from a folder
+        /// </summary>
+        /// <param name="folder"></param>
+        internal static async Task LoadPicFromFolderAsync(string folder)
+        {
+            // TODO add new function that can go to next/prev folder
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+            {
+                ChangeFolder(true);
+            });
+
+
+            // If searching subdirectories, it might freeze UI, so wrap it in task
+            await Task.Run(() =>
+            {
+                Pics = FileList(folder);
+            }).ConfigureAwait(false);
+
+            if (Pics?.Count > 0)
+            {
+                await LoadPicAtIndexAsync(0).ConfigureAwait(false);
+            }
+            else
+            {
+                await ReloadAsync(true).ConfigureAwait(false);
+            }
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, async () =>
+            {
+                if (GetImageSettingsMenu is not null)
+                {
+                    GetImageSettingsMenu.GoToPic.GoToPicBox.Text = (FolderIndex + 1).ToString(CultureInfo.CurrentCulture);
+                }
+
+                // Load new gallery values, if changing folder
+                if (GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryHorizontal || GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryVertical)
+                {
+                    if (GetPicGallery.Container.Children.Count == 0)
+                    {
+                        await GalleryLoad.Load().ConfigureAwait(false);
+                    }
+                }
+            });
+        }
+
+        /// <summary>
+        /// Update after FastPic() was used
+        /// </summary>
+        internal static async Task FastPicUpdateAsync()
+        {
+            // Make sure it's only updated when the key is actually held down
+            if (FastPicRunning == false)
+            {
+                return;
+            }
+
+            FastPicRunning = false;
+
+            Preloader.PreloadValue? preloadValue;
+
+            // Reset preloader values to prevent errors
+            if (Pics?.Count > Preloader.LoadBehind + Preloader.LoadInfront + 2)
+            {
+                Preloader.Clear();
+                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+            }
+            else
+            {
+                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+
+                if (preloadValue == null) // Error correctiom
+                {
+                    await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+                }
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+                }
+            }
+
+            if (preloadValue == null || preloadValue.bitmapSource == null)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () => { Error_Handling.Unload(); ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]); });
+                return;
+            }
+
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                UpdatePic(FolderIndex, preloadValue.bitmapSource);
+            });
+        }
+    }
+}

+ 2 - 697
PicView/ChangeImage/Navigation.cs

@@ -33,7 +33,7 @@ namespace PicView.ChangeImage
         /// <summary>
         /// Counter used to get current index
         /// </summary>
-        internal static int FolderIndex { get; private set; }
+        internal static int FolderIndex { get; set; }
 
         /// <summary>
         /// Backup of Previous file, if changed folder etc.
@@ -74,651 +74,6 @@ namespace PicView.ChangeImage
 
         #endregion Static fields
 
-        #region Load Pic from value
-
-        /// <summary>
-        /// Quickly load image and then update values
-        /// Only to be used from startup
-        /// </summary>
-        /// <param name="file"></param>
-        /// <returns></returns>
-        internal static async Task QuickLoad(string file)
-        {
-            if (File.Exists(file) == false)
-            {
-                await LoadPicFromString(file, false).ConfigureAwait(false);
-                return;
-            }
-
-            bool archive = false;
-            var pic = await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
-            if (pic is null)
-            {
-                archive = SupportedFiles.IsSupportedArchives(file);
-                if (archive == false)
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                    {
-                        Unload();
-                    });
-                    return;
-                }
-            }
-            else if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                {
-                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.Width;
-                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.Height;
-                });
-            }
-
-            await GetValues(file).ConfigureAwait(false);
-
-            switch (Pics.Count)
-            {
-                case > 0:
-                    FolderIndex = Pics.IndexOf(file);
-                    break;
-                default:
-                    FolderIndex = 0;
-                    break;
-            }
-
-            if (archive == false)
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
-                {
-                    UpdatePic(FolderIndex, pic);
-                    if (Properties.Settings.Default.AutoFitWindow)
-                    {
-                        UILogic.Sizing.WindowSizing.SetWindowBehavior();
-                    }
-                });
-
-                await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
-                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
-            }
-
-            if (FolderIndex > 0)
-            {
-                await Taskbar.Progress((double)FolderIndex / Pics.Count).ConfigureAwait(false);
-            }
-
-            if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
-            {
-                await GalleryLoad.Load().ConfigureAwait(false);
-            }
-
-            FreshStartup = false;
-
-            // Add recent files, except when browing archive
-            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > FolderIndex)
-            {
-                RecentFiles.Add(Pics?[FolderIndex]);
-            }
-        }
-
-        /// <summary>
-        /// Determine proper path from given string value
-        /// </summary>
-        /// <param name="path"></param>
-        /// <returns></returns>
-        internal static async Task LoadPicFromString(string path, bool checkExists = true)
-        {
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-            {
-                // Set Loading
-                SetLoadingString();
-
-                if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
-                {
-                    BitmapSource? bitmapSource = GetBitmapSourceThumb(path);
-                    if (bitmapSource != null)
-                    {
-                        ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
-                    }
-                }
-
-                // Don't allow image size to stretch the whole screen, fixes when opening new image from unloaded status
-                if (XWidth < 1)
-                {
-                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
-                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
-                }
-            });
-
-            if (checkExists && File.Exists(path))
-            {
-                // set up size so it feels better when starting application
-                await TryFitImageAsync(path).ConfigureAwait(false);
-
-                await LoadPiFromFileAsync(path).ConfigureAwait(false);
-
-                FreshStartup = false;
-            }
-            else
-            {
-                bool result = Uri.TryCreate(path, UriKind.Absolute, out Uri? uriResult)
-                    && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
-
-                if (result)
-                {
-                    await WebFunctions.PicWeb(path).ConfigureAwait(false);
-                    return;
-                }
-                else if (Base64.IsBase64String(path))
-                {
-                    await Pic64(path).ConfigureAwait(false);
-                    return;
-                }
-
-                if (FileFunctions.FilePathHasInvalidChars(path))
-                {
-                    FileFunctions.MakeValidFileName(path);
-                }
-
-                path = path.Replace("\"", "");
-                path = path.Trim();
-
-                if (File.Exists(path))
-                {
-                    await TryFitImageAsync(path).ConfigureAwait(false);
-
-                    await LoadPiFromFileAsync(path).ConfigureAwait(false);
-
-                    FreshStartup = false;
-                }
-
-                else if (Directory.Exists(path))
-                {
-                    await LoadPicFromFolderAsync(path).ConfigureAwait(false);
-
-                    FreshStartup = false;
-                }
-                else
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
-                    {
-                        Unload();
-                    });
-                }
-            }
-        }
-
-        /// <summary>
-        /// Loads a picture from a given file path and does extra error checking
-        /// </summary>
-        /// <param name="path"></param>
-        internal static async Task LoadPiFromFileAsync(string path)
-        {
-            bool folderChanged = false;
-
-            // If count not correct or just started, get values
-            if (Pics?.Count <= FolderIndex || FolderIndex < 0 || FreshStartup)
-            {
-                await GetValues(path).ConfigureAwait(false);
-            }
-            // If the file is in the same folder, navigate to it. If not, start manual loading procedure.
-            else if (!string.IsNullOrWhiteSpace(Pics?[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex]))
-            {
-                // Reset old values and get new
-                ChangeFolder(true);
-                await GetValues(path).ConfigureAwait(false);
-                folderChanged = true;
-            }
-            else if (Pics.Contains(path) == false)
-            {
-                await GetValues(path).ConfigureAwait(false);
-            }
-
-            if (Pics?.Count > 0)
-            {
-                FolderIndex = Pics.IndexOf(path);
-            }
-
-            if (!FreshStartup)
-            {
-                Preloader.Clear();
-            }
-
-            if (FolderIndex >= 0 && Pics?.Count > 0) // check if being extracted and need to wait for it instead
-            {
-                // Navigate to picture using obtained index
-                await LoadPicAtIndexAsync(FolderIndex).ConfigureAwait(false);
-            }
-
-            if (GetPicGallery is not null && folderChanged)
-            {
-                if (Properties.Settings.Default.FullscreenGalleryHorizontal || Properties.Settings.Default.FullscreenGalleryVertical)
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() =>
-                    {
-                        if (GetPicGallery == null)
-                        {
-                            return;
-                        }
-
-                        // Remove children before loading new
-                        if (GetPicGallery.Container.Children.Count > 0)
-                        {
-                            GetPicGallery.Container.Children.Clear();
-                        }
-                    }));
-
-                    // Load new gallery values, if changing folder
-                    await GalleryLoad.Load().ConfigureAwait(false);
-                }
-            }
-
-            FreshStartup = false;
-        }
-
-        /// <summary>
-        /// Loads image at specified index
-        /// </summary>
-        /// <param name="index">The index of file to load from Pics</param>
-        internal static async Task LoadPicAtIndexAsync(int index, bool resize = true)
-        {
-            if (Pics?.Count < index || Pics?.Count < 1)
-            {
-                return;
-            }
-
-            if (GetToolTipMessage is not null && GetToolTipMessage.IsVisible)
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                {
-                    GetToolTipMessage.Visibility = Visibility.Hidden;
-                });
-            }
-
-            FolderIndex = index;
-            var preloadValue = Preloader.Get(Navigation.Pics[index]);
-
-            // Initate loading behavior, if needed
-            if (preloadValue == null || preloadValue.isLoading)
-            {
-                // Show a thumbnail while loading
-                BitmapSource? thumb = null;
-                
-                if (GalleryFunctions.IsHorizontalFullscreenOpen == false || GalleryFunctions.IsVerticalFullscreenOpen == false)
-                {
-                    thumb = GetBitmapSourceThumb(Pics[FolderIndex]);
-                }
-
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                {
-                    if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
-                    {
-                        thumb = GetThumb(index);
-                    }
-
-                    if (FreshStartup)
-                    {
-                        // Set loading from translation service
-                        SetLoadingString();
-                        FreshStartup = false;
-                    }
-                    else
-                    {
-                        var image = Application.Current.Resources["Image"] as string;
-
-                        ConfigureWindows.GetMainWindow.TitleText.ToolTip =
-                        ConfigureWindows.GetMainWindow.Title =
-                        ConfigureWindows.GetMainWindow.TitleText.Text
-                        = $"{image} {index + 1} / {Pics?.Count}";
-                    }
-
-                    if (thumb != null)
-                    {
-                        ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
-                    }
-                });
-
-                if (FastPicRunning) // Holding down button is too fast and will be laggy when not just loading thumbnails
-                {
-                    return;
-                }
-                while (preloadValue != null && preloadValue.isLoading)
-                {
-                    // Wait for finnished result
-                    await Task.Delay(5).ConfigureAwait(false);
-
-                    // Make loading skippable
-                    if (FolderIndex != index)
-                    {
-                        // Start preloading when browsing very fast to catch up
-                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
-                        return;
-                    }
-                }
-                if (preloadValue == null) // Error correctiom
-                {
-                    await Preloader.AddAsync(index).ConfigureAwait(false);
-                    preloadValue = Preloader.Get(Navigation.Pics[index]);
-                }
-            }
-
-            // Make loading skippable
-            if (FolderIndex != index)
-            {
-                // Start preloading when browsing very fast to catch up
-                if (Preloader.IsRunning == false)
-                {
-                    await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
-                }
-
-                if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                    {
-                        GalleryNavigation.FullscreenGalleryNavigation();
-                    });
-                }
-                return;
-            }
-
-            // Check if works, if not show error message
-            if (preloadValue is not null)
-            {
-                while (preloadValue != null && preloadValue.isLoading)
-                {
-                    // Wait for finnished result
-                    await Task.Delay(5).ConfigureAwait(false);
-
-                    // Make loading skippable
-                    if (FolderIndex != index)
-                    {
-                        // Start preloading when browsing very fast to catch up
-                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
-                        return;
-                    }
-                }
-                if (preloadValue is null) // Error correctiom
-                {
-                    await Preloader.AddAsync(index).ConfigureAwait(false);
-                    preloadValue = Preloader.Get(Navigation.Pics[index]);
-                }
-                if (preloadValue is null)
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                    {
-                        Error_Handling.Unload();
-                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
-                    });
-                    return;
-                }
-                if (preloadValue.bitmapSource == null)
-                {
-                    preloadValue = new Preloader.PreloadValue(ImageFunctions.ImageErrorMessage(), false);
-
-                    if (preloadValue == null || preloadValue.bitmapSource == null)
-                    {
-                        await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                        {
-                            Error_Handling.Unload();
-                            ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
-                        });
-                        return;
-                    }
-                }
-            }
-            else
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                {
-                    Error_Handling.Unload();
-                    ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
-                });
-                return;
-            }
-
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
-            {
-                UpdatePic(index, preloadValue.bitmapSource, resize);
-            });
-
-            // Update PicGallery selected item, if needed
-            if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-                {
-                    GalleryNavigation.FullscreenGalleryNavigation();
-                });
-            }
-
-            await ImageInfo.UpdateValuesAsync(Pics?[FolderIndex]).ConfigureAwait(false);
-
-            if (Pics?.Count > 1)
-            {
-                if (Preloader.IsRunning == false)
-                {
-                    await Task.Run(() => Preloader.PreLoad(index)).ConfigureAwait(false);
-                }
-
-                if (FolderIndex == index)
-                {
-                    await Taskbar.Progress((double)index / Pics.Count).ConfigureAwait(false);
-                }
-            }
-
-            // Add recent files, except when browing archive
-            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > index)
-            {
-                RecentFiles.Add(Pics?[index]);
-            }
-        }
-
-        /// <summary>
-        /// Update picture, size it and set the title from index
-        /// </summary>
-        /// <param name="index"></param>
-        /// <param name="bitmapSource"></param>
-        internal static void UpdatePic(int index, BitmapSource bitmapSource, bool resise = true)
-        {
-            // Scroll to top if scroll enabled
-            if (Properties.Settings.Default.ScrollEnabled)
-            {
-                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;
-                if (GetQuickSettingsMenu is not null && GetQuickSettingsMenu.FlipButton is not null)
-                {
-                    GetQuickSettingsMenu.FlipButton.TheButton.IsChecked = false;
-                }
-
-                ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
-            }
-
-            // Loads gif from XamlAnimatedGif if neccesary
-            string? ext = Path.GetExtension(Pics?[index]);
-            if (ext is not null && ext.Equals(".gif", StringComparison.OrdinalIgnoreCase))
-            {
-                XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(Pics?[index]));
-            }
-            else
-            {
-                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
-            }
-
-            if (resise)
-            {
-                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
-            }
-
-            SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index);
-        }
-
-        /// <summary>
-        /// Update picture, size it and set the title from string
-        /// </summary>
-        /// <param name="imageName"></param>
-        /// <param name="bitmapSource"></param>
-        internal static async Task UpdatePic(string imageName, BitmapSource bitmapSource)
-        {
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
-            {
-                Unload();
-
-                if (Properties.Settings.Default.ScrollEnabled)
-                {
-                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
-                }
-
-                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
-
-                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
-                SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
-
-                CloseToolTipMessage();
-            });
-
-            await Taskbar.NoProgress().ConfigureAwait(false);
-            FolderIndex = 0;
-
-            await ImageInfo.UpdateValuesAsync(imageName).ConfigureAwait(false);
-        }
-
-        /// <summary>
-        /// Load a picture from a prepared bitmap
-        /// </summary>
-        /// <param name="pic"></param>
-        /// <param name="imageName"></param>
-        internal static void Pic(BitmapSource bitmap, string imageName)
-        {
-            _ = UpdatePic(imageName, bitmap);
-
-            DeleteFiles.DeleteTempFiles();
-        }
-
-        /// <summary>
-        /// Load a picture from a prepared string
-        /// </summary>
-        /// <param name="pic"></param>
-        /// <param name="imageName"></param>
-        internal static async Task PicAsync(string file, string imageName, bool isGif)
-        {
-            BitmapSource? bitmapSource = isGif ? null : await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, async () =>
-            {
-                if (Properties.Settings.Default.ScrollEnabled)
-                {
-                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
-                }
-
-                if (isGif)
-                {
-                    Size? imageSize = await ImageFunctions.ImageSizeAsync(file).ConfigureAwait(true);
-                    if (imageSize.HasValue)
-                    {
-                        FitImage(imageSize.Value.Width, imageSize.Value.Height);
-                        SetTitleString((int)imageSize.Value.Width, (int)imageSize.Value.Height, imageName);
-                    }
-                    XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(file));
-                }
-                else if (bitmapSource != null)
-                {
-                    ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
-                    SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
-                    FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
-                }
-                else
-                {
-                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () => 
-                    {
-                        Error_Handling.Unload(); 
-                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]); 
-                    });
-                    return;
-                }
-
-                CloseToolTipMessage();
-            });
-
-            await Taskbar.NoProgress().ConfigureAwait(false);
-            FolderIndex = 0;
-
-            await ImageInfo.UpdateValuesAsync(file).ConfigureAwait(false);
-
-            DeleteFiles.DeleteTempFiles();
-        }
-
-        /// <summary>
-        /// Load a picture from a base64
-        /// </summary>
-        /// <param name="pic"></param>
-        /// <param name="imageName"></param>
-        internal static async Task Pic64(string base64string)
-        {
-            if (string.IsNullOrEmpty(base64string))
-            {
-                return;
-            }
-            var pic = await Base64.Base64StringToBitmap(base64string).ConfigureAwait(false);
-            if (pic == null)
-            {
-                return;
-            }
-            if (Application.Current.Resources["Base64Image"] is not string b64)
-            {
-                return;
-            }
-            await UpdatePic(b64, pic).ConfigureAwait(false);
-        }
-
-        /// <summary>
-        /// Handle logic if user wants to load from a folder
-        /// </summary>
-        /// <param name="folder"></param>
-        internal static async Task LoadPicFromFolderAsync(string folder)
-        {
-            // TODO add new function that can go to next/prev folder
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
-            {
-                ChangeFolder(true);
-            });
-
-
-            // If searching subdirectories, it might freeze UI, so wrap it in task
-            await Task.Run(() =>
-            {
-                Pics = FileList(folder);
-            }).ConfigureAwait(false);
-
-            if (Pics?.Count > 0)
-            {
-                await LoadPicAtIndexAsync(0).ConfigureAwait(false);
-            }
-            else
-            {
-                await ReloadAsync(true).ConfigureAwait(false);
-            }
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, async () =>
-            {
-                if (GetImageSettingsMenu is not null)
-                {
-                    GetImageSettingsMenu.GoToPic.GoToPicBox.Text = (FolderIndex + 1).ToString(CultureInfo.CurrentCulture);
-                }
-
-                // Load new gallery values, if changing folder
-                if (GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryHorizontal || GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryVertical)
-                {
-                    if (GetPicGallery.Container.Children.Count == 0)
-                    {
-                        await GalleryLoad.Load().ConfigureAwait(false);
-                    }
-                }
-            });
-        }
-
-        #endregion
-
         #region Change navigation values
 
         /// <summary>
@@ -798,7 +153,7 @@ namespace PicView.ChangeImage
             }
 
             // Go to the image!
-            await LoadPicAtIndexAsync(startingpoint).ConfigureAwait(false);
+            await LoadPic.LoadPicAtIndexAsync(startingpoint).ConfigureAwait(false);
         }
 
         /// <summary>
@@ -853,56 +208,6 @@ namespace PicView.ChangeImage
             }
         }
 
-        /// <summary>
-        /// Update after FastPic() was used
-        /// </summary>
-        internal static async Task FastPicUpdateAsync()
-        {
-            // Make sure it's only updated when the key is actually held down
-            if (FastPicRunning == false)
-            {
-                return;
-            }
-
-            FastPicRunning = false;
-
-            Preloader.PreloadValue? preloadValue;
-
-            // Reset preloader values to prevent errors
-            if (Pics?.Count > Preloader.LoadBehind + Preloader.LoadInfront + 2)
-            {
-                Preloader.Clear();
-                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
-                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
-            }
-            else
-            {
-                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
-
-                if (preloadValue == null) // Error correctiom
-                {
-                    await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
-                    preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
-                }
-                while (preloadValue != null && preloadValue.isLoading)
-                {
-                    // Wait for finnished result
-                    await Task.Delay(5).ConfigureAwait(false);
-                }
-            }
-
-            if (preloadValue == null || preloadValue.bitmapSource == null)
-            {
-                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () => { Error_Handling.Unload(); ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]); });
-                return;
-            }
-
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
-            {
-                UpdatePic(FolderIndex, preloadValue.bitmapSource);
-            });
-        }
-
         #endregion Change navigation values
     }
 }

+ 717 - 0
PicView/ChangeImage/UpdateImageValues.cs

@@ -0,0 +1,717 @@
+using PicView.FileHandling;
+using PicView.ImageHandling;
+using PicView.PicGallery;
+using PicView.SystemIntegration;
+using PicView.UILogic;
+using System;
+using System.Globalization;
+using System.IO;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using static PicView.ChangeImage.Error_Handling;
+using static PicView.ChangeImage.Navigation;
+using static PicView.FileHandling.ArchiveExtraction;
+using static PicView.FileHandling.FileLists;
+using static PicView.ImageHandling.Thumbnails;
+using static PicView.UILogic.SetTitle;
+using static PicView.UILogic.Sizing.ScaleImage;
+using static PicView.UILogic.Tooltip;
+using static PicView.UILogic.UC;
+
+namespace PicView.ChangeImage
+{
+    internal static class UpdateImageValues
+    {
+        #region Load Pic from value
+
+        /// <summary>
+        /// Quickly load image and then update values
+        /// Only to be used from startup
+        /// </summary>
+        /// <param name="file"></param>
+        /// <returns></returns>
+        internal static async Task QuickLoad(string file)
+        {
+            if (File.Exists(file) == false)
+            {
+                await LoadPicFromString(file, false).ConfigureAwait(false);
+                return;
+            }
+
+            bool archive = false;
+            var pic = await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
+            if (pic is null)
+            {
+                archive = SupportedFiles.IsSupportedArchives(file);
+                if (archive == false)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Unload();
+                    });
+                    return;
+                }
+            }
+            else if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.Width;
+                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.Height;
+                });
+            }
+
+            await GetValues(file).ConfigureAwait(false);
+
+            switch (Pics.Count)
+            {
+                case > 0:
+                    FolderIndex = Pics.IndexOf(file);
+                    break;
+                default:
+                    FolderIndex = 0;
+                    break;
+            }
+
+            if (archive == false)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+                {
+                    UpdatePic(FolderIndex, pic);
+                    if (Properties.Settings.Default.AutoFitWindow)
+                    {
+                        UILogic.Sizing.WindowSizing.SetWindowBehavior();
+                    }
+                });
+
+                await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+            }
+
+            if (FolderIndex > 0)
+            {
+                await Taskbar.Progress((double)FolderIndex / Pics.Count).ConfigureAwait(false);
+            }
+
+            if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
+            {
+                await GalleryLoad.Load().ConfigureAwait(false);
+            }
+
+            FreshStartup = false;
+
+            // Add recent files, except when browing archive
+            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > FolderIndex)
+            {
+                RecentFiles.Add(Pics?[FolderIndex]);
+            }
+        }
+
+        /// <summary>
+        /// Determine proper path from given string value
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        internal static async Task LoadPicFromString(string path, bool checkExists = true)
+        {
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+            {
+                // Set Loading
+                SetLoadingString();
+
+                if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
+                {
+                    BitmapSource? bitmapSource = GetBitmapSourceThumb(path);
+                    if (bitmapSource != null)
+                    {
+                        ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+                    }
+                }
+
+                // Don't allow image size to stretch the whole screen, fixes when opening new image from unloaded status
+                if (XWidth < 1)
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
+                    ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
+                }
+            });
+
+            if (checkExists && File.Exists(path))
+            {
+                // set up size so it feels better when starting application
+                await TryFitImageAsync(path).ConfigureAwait(false);
+
+                await LoadPiFromFileAsync(path).ConfigureAwait(false);
+
+                FreshStartup = false;
+            }
+            else
+            {
+                bool result = Uri.TryCreate(path, UriKind.Absolute, out Uri? uriResult)
+                    && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
+
+                if (result)
+                {
+                    await WebFunctions.PicWeb(path).ConfigureAwait(false);
+                    return;
+                }
+                else if (Base64.IsBase64String(path))
+                {
+                    await Pic64(path).ConfigureAwait(false);
+                    return;
+                }
+
+                if (FileFunctions.FilePathHasInvalidChars(path))
+                {
+                    FileFunctions.MakeValidFileName(path);
+                }
+
+                path = path.Replace("\"", "");
+                path = path.Trim();
+
+                if (File.Exists(path))
+                {
+                    await TryFitImageAsync(path).ConfigureAwait(false);
+
+                    await LoadPiFromFileAsync(path).ConfigureAwait(false);
+
+                    FreshStartup = false;
+                }
+
+                else if (Directory.Exists(path))
+                {
+                    await LoadPicFromFolderAsync(path).ConfigureAwait(false);
+
+                    FreshStartup = false;
+                }
+                else
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
+                    {
+                        Unload();
+                    });
+                }
+            }
+        }
+
+        /// <summary>
+        /// Loads a picture from a given file path and does extra error checking
+        /// </summary>
+        /// <param name="path"></param>
+        internal static async Task LoadPiFromFileAsync(string path)
+        {
+            bool folderChanged = false;
+
+            // If count not correct or just started, get values
+            if (Pics?.Count <= FolderIndex || FolderIndex < 0 || FreshStartup)
+            {
+                await GetValues(path).ConfigureAwait(false);
+            }
+            // If the file is in the same folder, navigate to it. If not, start manual loading procedure.
+            else if (!string.IsNullOrWhiteSpace(Pics?[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex]))
+            {
+                // Reset old values and get new
+                ChangeFolder(true);
+                await GetValues(path).ConfigureAwait(false);
+                folderChanged = true;
+            }
+            else if (Pics.Contains(path) == false)
+            {
+                await GetValues(path).ConfigureAwait(false);
+            }
+
+            if (Pics?.Count > 0)
+            {
+                FolderIndex = Pics.IndexOf(path);
+            }
+
+            if (!FreshStartup)
+            {
+                Preloader.Clear();
+            }
+
+            if (FolderIndex >= 0 && Pics?.Count > 0) // check if being extracted and need to wait for it instead
+            {
+                // Navigate to picture using obtained index
+                await LoadPicAtIndexAsync(FolderIndex).ConfigureAwait(false);
+            }
+
+            if (GetPicGallery is not null && folderChanged)
+            {
+                if (Properties.Settings.Default.FullscreenGalleryHorizontal || Properties.Settings.Default.FullscreenGalleryVertical)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() =>
+                    {
+                        if (GetPicGallery == null)
+                        {
+                            return;
+                        }
+
+                        // Remove children before loading new
+                        if (GetPicGallery.Container.Children.Count > 0)
+                        {
+                            GetPicGallery.Container.Children.Clear();
+                        }
+                    }));
+
+                    // Load new gallery values, if changing folder
+                    await GalleryLoad.Load().ConfigureAwait(false);
+                }
+            }
+
+            FreshStartup = false;
+        }
+
+        /// <summary>
+        /// Loads image at specified index
+        /// </summary>
+        /// <param name="index">The index of file to load from Pics</param>
+        internal static async Task LoadPicAtIndexAsync(int index, bool resize = true)
+        {
+            if (Pics?.Count < index || Pics?.Count < 1)
+            {
+                return;
+            }
+
+            if (GetToolTipMessage is not null && GetToolTipMessage.IsVisible)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    GetToolTipMessage.Visibility = Visibility.Hidden;
+                });
+            }
+
+            FolderIndex = index;
+            var preloadValue = Preloader.Get(Navigation.Pics[index]);
+
+            // Initate loading behavior, if needed
+            if (preloadValue == null || preloadValue.isLoading)
+            {
+                // Show a thumbnail while loading
+                BitmapSource? thumb = null;
+
+                if (GalleryFunctions.IsHorizontalFullscreenOpen == false || GalleryFunctions.IsVerticalFullscreenOpen == false)
+                {
+                    thumb = GetBitmapSourceThumb(Pics[FolderIndex]);
+                }
+
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+                    {
+                        thumb = GetThumb(index);
+                    }
+
+                    if (FreshStartup)
+                    {
+                        // Set loading from translation service
+                        SetLoadingString();
+                        FreshStartup = false;
+                    }
+                    else
+                    {
+                        var image = Application.Current.Resources["Image"] as string;
+
+                        ConfigureWindows.GetMainWindow.TitleText.ToolTip =
+                        ConfigureWindows.GetMainWindow.Title =
+                        ConfigureWindows.GetMainWindow.TitleText.Text
+                        = $"{image} {index + 1} / {Pics?.Count}";
+                    }
+
+                    if (thumb != null)
+                    {
+                        ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
+                    }
+                });
+
+                if (FastPicRunning) // Holding down button is too fast and will be laggy when not just loading thumbnails
+                {
+                    return;
+                }
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+
+                    // Make loading skippable
+                    if (FolderIndex != index)
+                    {
+                        // Start preloading when browsing very fast to catch up
+                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                        return;
+                    }
+                }
+                if (preloadValue == null) // Error correctiom
+                {
+                    await Preloader.AddAsync(index).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[index]);
+                }
+            }
+
+            // Make loading skippable
+            if (FolderIndex != index)
+            {
+                // Start preloading when browsing very fast to catch up
+                if (Preloader.IsRunning == false)
+                {
+                    await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                }
+
+                if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        GalleryNavigation.FullscreenGalleryNavigation();
+                    });
+                }
+                return;
+            }
+
+            // Check if works, if not show error message
+            if (preloadValue is not null)
+            {
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+
+                    // Make loading skippable
+                    if (FolderIndex != index)
+                    {
+                        // Start preloading when browsing very fast to catch up
+                        await Task.Run(() => Preloader.PreLoad(FolderIndex)).ConfigureAwait(false);
+                        return;
+                    }
+                }
+                if (preloadValue is null) // Error correctiom
+                {
+                    await Preloader.AddAsync(index).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[index]);
+                }
+                if (preloadValue is null)
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Error_Handling.Unload();
+                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                    });
+                    return;
+                }
+                if (preloadValue.bitmapSource == null)
+                {
+                    preloadValue = new Preloader.PreloadValue(ImageFunctions.ImageErrorMessage(), false);
+
+                    if (preloadValue == null || preloadValue.bitmapSource == null)
+                    {
+                        await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                        {
+                            Error_Handling.Unload();
+                            ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                        });
+                        return;
+                    }
+                }
+            }
+            else
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    Error_Handling.Unload();
+                    ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                });
+                return;
+            }
+
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                UpdatePic(index, preloadValue.bitmapSource, resize);
+            });
+
+            // Update PicGallery selected item, if needed
+            if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                {
+                    GalleryNavigation.FullscreenGalleryNavigation();
+                });
+            }
+
+            await ImageInfo.UpdateValuesAsync(Pics?[FolderIndex]).ConfigureAwait(false);
+
+            if (Pics?.Count > 1)
+            {
+                if (Preloader.IsRunning == false)
+                {
+                    await Task.Run(() => Preloader.PreLoad(index)).ConfigureAwait(false);
+                }
+
+                if (FolderIndex == index)
+                {
+                    await Taskbar.Progress((double)index / Pics.Count).ConfigureAwait(false);
+                }
+            }
+
+            // Add recent files, except when browing archive
+            if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > index)
+            {
+                RecentFiles.Add(Pics?[index]);
+            }
+        }
+
+        /// <summary>
+        /// Handle logic if user wants to load from a folder
+        /// </summary>
+        /// <param name="folder"></param>
+        internal static async Task LoadPicFromFolderAsync(string folder)
+        {
+            // TODO add new function that can go to next/prev folder
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+            {
+                ChangeFolder(true);
+            });
+
+
+            // If searching subdirectories, it might freeze UI, so wrap it in task
+            await Task.Run(() =>
+            {
+                Pics = FileList(folder);
+            }).ConfigureAwait(false);
+
+            if (Pics?.Count > 0)
+            {
+                await LoadPicAtIndexAsync(0).ConfigureAwait(false);
+            }
+            else
+            {
+                await ReloadAsync(true).ConfigureAwait(false);
+            }
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Background, async () =>
+            {
+                if (GetImageSettingsMenu is not null)
+                {
+                    GetImageSettingsMenu.GoToPic.GoToPicBox.Text = (FolderIndex + 1).ToString(CultureInfo.CurrentCulture);
+                }
+
+                // Load new gallery values, if changing folder
+                if (GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryHorizontal || GetPicGallery != null && Properties.Settings.Default.FullscreenGalleryVertical)
+                {
+                    if (GetPicGallery.Container.Children.Count == 0)
+                    {
+                        await GalleryLoad.Load().ConfigureAwait(false);
+                    }
+                }
+            });
+        }
+
+        /// <summary>
+        /// Update after FastPic() was used
+        /// </summary>
+        internal static async Task FastPicUpdateAsync()
+        {
+            // Make sure it's only updated when the key is actually held down
+            if (FastPicRunning == false)
+            {
+                return;
+            }
+
+            FastPicRunning = false;
+
+            Preloader.PreloadValue? preloadValue;
+
+            // Reset preloader values to prevent errors
+            if (Pics?.Count > Preloader.LoadBehind + Preloader.LoadInfront + 2)
+            {
+                Preloader.Clear();
+                await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+            }
+            else
+            {
+                preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+
+                if (preloadValue == null) // Error correctiom
+                {
+                    await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+                    preloadValue = Preloader.Get(Navigation.Pics[FolderIndex]);
+                }
+                while (preloadValue != null && preloadValue.isLoading)
+                {
+                    // Wait for finnished result
+                    await Task.Delay(5).ConfigureAwait(false);
+                }
+            }
+
+            if (preloadValue == null || preloadValue.bitmapSource == null)
+            {
+                await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () => { Error_Handling.Unload(); ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]); });
+                return;
+            }
+
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                UpdatePic(FolderIndex, preloadValue.bitmapSource);
+            });
+        }
+
+        #endregion
+
+        #region Set values
+        /// <summary>
+        /// Update picture, size it and set the title from index
+        /// </summary>
+        /// <param name="index"></param>
+        /// <param name="bitmapSource"></param>
+        internal static void UpdatePic(int index, BitmapSource bitmapSource, bool resise = true)
+        {
+            // Scroll to top if scroll enabled
+            if (Properties.Settings.Default.ScrollEnabled)
+            {
+                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;
+                if (GetQuickSettingsMenu is not null && GetQuickSettingsMenu.FlipButton is not null)
+                {
+                    GetQuickSettingsMenu.FlipButton.TheButton.IsChecked = false;
+                }
+
+                ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
+            }
+
+            // Loads gif from XamlAnimatedGif if neccesary
+            string? ext = Path.GetExtension(Pics?[index]);
+            if (ext is not null && ext.Equals(".gif", StringComparison.OrdinalIgnoreCase))
+            {
+                XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(Pics?[index]));
+            }
+            else
+            {
+                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+            }
+
+            if (resise)
+            {
+                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+            }
+
+            SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index);
+        }
+
+        /// <summary>
+        /// Update picture, size it and set the title from string
+        /// </summary>
+        /// <param name="imageName"></param>
+        /// <param name="bitmapSource"></param>
+        internal static async Task UpdatePic(string imageName, BitmapSource bitmapSource)
+        {
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, () =>
+            {
+                Unload();
+
+                if (Properties.Settings.Default.ScrollEnabled)
+                {
+                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
+                }
+
+                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+
+                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+                SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
+
+                CloseToolTipMessage();
+            });
+
+            await Taskbar.NoProgress().ConfigureAwait(false);
+            FolderIndex = 0;
+
+            await ImageInfo.UpdateValuesAsync(imageName).ConfigureAwait(false);
+        }
+
+        /// <summary>
+        /// Load a picture from a prepared string
+        /// </summary>
+        /// <param name="pic"></param>
+        /// <param name="imageName"></param>
+        internal static async Task PicAsync(string file, string imageName, bool isGif)
+        {
+            BitmapSource? bitmapSource = isGif ? null : await ImageDecoder.RenderToBitmapSource(file).ConfigureAwait(false);
+            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, async () =>
+            {
+                if (Properties.Settings.Default.ScrollEnabled)
+                {
+                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
+                }
+
+                if (isGif)
+                {
+                    Size? imageSize = await ImageFunctions.ImageSizeAsync(file).ConfigureAwait(true);
+                    if (imageSize.HasValue)
+                    {
+                        FitImage(imageSize.Value.Width, imageSize.Value.Height);
+                        SetTitleString((int)imageSize.Value.Width, (int)imageSize.Value.Height, imageName);
+                    }
+                    XamlAnimatedGif.AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(file));
+                }
+                else if (bitmapSource != null)
+                {
+                    ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
+                    SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
+                    FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
+                }
+                else
+                {
+                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
+                    {
+                        Error_Handling.Unload();
+                        ShowTooltipMessage(Application.Current.Resources["UnexpectedError"]);
+                    });
+                    return;
+                }
+
+                CloseToolTipMessage();
+            });
+
+            await Taskbar.NoProgress().ConfigureAwait(false);
+            FolderIndex = 0;
+
+            await ImageInfo.UpdateValuesAsync(file).ConfigureAwait(false);
+
+            DeleteFiles.DeleteTempFiles();
+        }
+
+        /// <summary>
+        /// Load a picture from a base64
+        /// </summary>
+        /// <param name="pic"></param>
+        /// <param name="imageName"></param>
+        internal static async Task Pic64(string base64string)
+        {
+            if (string.IsNullOrEmpty(base64string))
+            {
+                return;
+            }
+            var pic = await Base64.Base64StringToBitmap(base64string).ConfigureAwait(false);
+            if (pic == null)
+            {
+                return;
+            }
+            if (Application.Current.Resources["Base64Image"] is not string b64)
+            {
+                return;
+            }
+            await UpdatePic(b64, pic).ConfigureAwait(false);
+        }
+
+        #endregion
+
+        #region Skip logic
+
+        #endregion
+    }
+}

+ 2 - 4
PicView/FileHandling/ArchiveExtraction.cs

@@ -1,7 +1,5 @@
 using PicView.ChangeImage;
-using PicView.ImageHandling;
 using PicView.SystemIntegration;
-using PicView.UILogic;
 using System;
 using System.Diagnostics;
 using System.IO;
@@ -141,7 +139,7 @@ namespace PicView.FileHandling
                 }
                 if (Pics.Count >= 2 && !x.HasExited)
                 {
-                    await LoadPicAtIndexAsync(0).ConfigureAwait(false);
+                    await LoadPic.LoadPicAtIndexAsync(0).ConfigureAwait(false);
                 }
             };
             x.Exited += async delegate
@@ -150,7 +148,7 @@ namespace PicView.FileHandling
                 {
                     if (Navigation.FolderIndex > 0)
                     {
-                        await LoadPicAtIndexAsync(0).ConfigureAwait(false);
+                        await LoadPic.LoadPicAtIndexAsync(0).ConfigureAwait(false);
                     }
 
                     // Add zipped files as recent file

+ 5 - 4
PicView/FileHandling/Copy-paste.cs

@@ -1,4 +1,5 @@
-using PicView.ImageHandling;
+using PicView.ChangeImage;
+using PicView.ImageHandling;
 using PicView.UILogic;
 using System;
 using System.IO;
@@ -97,7 +98,7 @@ namespace PicView.FileHandling
 
                 if (files != null)
                 {
-                    await LoadPicFromString(files[0]).ConfigureAwait(false);
+                    await LoadPic.LoadPicFromString(files[0]).ConfigureAwait(false);
 
                     if (files.Length > 1)
                     {
@@ -113,7 +114,7 @@ namespace PicView.FileHandling
             // Clipboard Image
             if (Clipboard.ContainsImage())
             {
-                Pic(Clipboard.GetImage(), (string)Application.Current.Resources["ClipboardImage"]);
+                LoadPic.Pic(Clipboard.GetImage(), (string)Application.Current.Resources["ClipboardImage"]);
                 return;
             }
 
@@ -126,7 +127,7 @@ namespace PicView.FileHandling
                 return;
             }
 
-            await LoadPicFromString(s).ConfigureAwait(false);
+            await LoadPic.LoadPicFromString(s).ConfigureAwait(false);
         }
 
         /// <summary>

+ 1 - 1
PicView/FileHandling/FileFunctions.cs

@@ -108,7 +108,7 @@ namespace PicView.FileHandling
             {
                 if (ChangeImage.Navigation.Pics.Count < 1)
                 {
-                    await ChangeImage.Navigation.LoadPiFromFileAsync(newPath).ConfigureAwait(false);
+                    await ChangeImage.LoadPic.LoadPiFromFileAsync(newPath).ConfigureAwait(false);
                     return false;
                 }
 

+ 2 - 1
PicView/FileHandling/Open_Save.cs

@@ -1,4 +1,5 @@
 using Microsoft.Win32;
+using PicView.ChangeImage;
 using PicView.ImageHandling;
 using PicView.UILogic;
 using PicView.UILogic.Sizing;
@@ -93,7 +94,7 @@ namespace PicView.FileHandling
             };
             if (dlg.ShowDialog().HasValue)
             {
-                await LoadPiFromFileAsync(dlg.FileName).ConfigureAwait(false);
+                await LoadPic.LoadPiFromFileAsync(dlg.FileName).ConfigureAwait(false);
             }
             else
             {

+ 5 - 4
PicView/FileHandling/RecentFiles.cs

@@ -1,4 +1,5 @@
-using System;
+using PicView.ChangeImage;
+using System;
 using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
@@ -146,7 +147,7 @@ namespace PicView.FileHandling
                         Height = double.NaN,
                         HorizontalAlignment = HorizontalAlignment.Left,
                     };
-                    button.Click += async delegate { await LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
+                    button.Click += async delegate { await LoadPic.LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
                     menuItem.MouseEnter += (_, _) => button.Foreground = new SolidColorBrush(Colors.White);
                     var txt = (SolidColorBrush)Application.Current.Resources["MainColorBrush"];
                     menuItem.MouseLeave += (_, _) => button.Foreground = txt;
@@ -155,7 +156,7 @@ namespace PicView.FileHandling
                     var ext = Path.GetExtension(item);
                     var ext5 = !string.IsNullOrWhiteSpace(ext) && ext.Length >= 5 ? ext.Substring(0, 5) : ext;
                     menuItem.InputGestureText = ext5;
-                    menuItem.Click += async delegate { await LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
+                    menuItem.Click += async delegate { await LoadPic.LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
                 }
                 return;
             }
@@ -207,7 +208,7 @@ namespace PicView.FileHandling
                 var txt = (SolidColorBrush)Application.Current.Resources["MainColorBrush"];
                 menuItem.MouseLeave += (_, _) => button.Foreground = txt;
                 // Set tooltip as argument to avoid subscribing and unsubscribing to events
-                menuItem.Click += async delegate { await LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
+                menuItem.Click += async delegate { await LoadPic.LoadPiFromFileAsync(menuItem.ToolTip.ToString()).ConfigureAwait(false); };
                 var ext = Path.GetExtension(item);
                 var ext5 = !string.IsNullOrWhiteSpace(ext) && ext.Length >= 5 ? ext.Substring(0, 5) : ext;
                 menuItem.InputGestureText = ext5;

+ 1 - 1
PicView/FileHandling/WebFunctions.cs

@@ -35,7 +35,7 @@ namespace PicView.FileHandling
                 var destination = await DownloadData(url, true).ConfigureAwait(false);
                 var isGif = Path.GetExtension(url).Contains(".gif", StringComparison.OrdinalIgnoreCase);
 
-                await PicAsync(destination, url, isGif).ConfigureAwait(false);
+                await LoadPic.PicAsync(destination, url, isGif).ConfigureAwait(false);
 
                 await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
                 {

+ 3 - 3
PicView/PicGallery/GalleryClick.cs

@@ -1,4 +1,5 @@
-using PicView.UILogic;
+using PicView.ChangeImage;
+using PicView.UILogic;
 using System;
 using System.Threading.Tasks;
 using System.Windows;
@@ -7,7 +8,6 @@ using System.Windows.Media;
 using System.Windows.Media.Animation;
 using static PicView.ChangeImage.Navigation;
 using static PicView.ImageHandling.Thumbnails;
-using static PicView.PicGallery.GalleryFunctions;
 using static PicView.UILogic.Sizing.ScaleImage;
 using static PicView.UILogic.UC;
 
@@ -149,7 +149,7 @@ namespace PicView.PicGallery
                 GalleryNavigation.SelectedGalleryItem = id;
             });
             // Change image
-            await LoadPicAtIndexAsync(id, resize).ConfigureAwait(false);
+            await LoadPic.LoadPicAtIndexAsync(id, resize).ConfigureAwait(false);
         }
     }
 }

+ 1 - 1
PicView/Shortcuts/MainShortcuts.cs

@@ -590,7 +590,7 @@ namespace PicView.Shortcuts
                     {
                         return;
                     }
-                    await FastPicUpdateAsync().ConfigureAwait(false);
+                    await ChangeImage.LoadPic.FastPicUpdateAsync().ConfigureAwait(false);
                     return;
 
                 default: break;

+ 4 - 3
PicView/UILogic/DragAndDrop/Image_DragAndDrop.cs

@@ -1,4 +1,5 @@
-using PicView.FileHandling;
+using PicView.ChangeImage;
+using PicView.FileHandling;
 using PicView.ProcessHandling;
 using PicView.Views.UserControls;
 using PicView.Views.UserControls.Misc;
@@ -158,7 +159,7 @@ namespace PicView.UILogic.DragAndDrop
             {
                 if (Directory.Exists(files[0]))
                 {
-                    await LoadPicFromFolderAsync(files[0]).ConfigureAwait(false);
+                    await LoadPic.LoadPicFromFolderAsync(files[0]).ConfigureAwait(false);
                 }
                 return;
             }
@@ -174,7 +175,7 @@ namespace PicView.UILogic.DragAndDrop
                 }
             }
 
-            await LoadPicFromString(files[0]).ConfigureAwait(false);
+            await LoadPic.LoadPicFromString(files[0]).ConfigureAwait(false);
 
             await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, () =>
             {

+ 1 - 3
PicView/UILogic/Loading/StartLoading.cs

@@ -1,14 +1,12 @@
 using PicView.FileHandling;
 using PicView.PicGallery;
 using PicView.SystemIntegration;
-using PicView.Translations;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Interop;
 using static PicView.ChangeImage.Error_Handling;
 using static PicView.ChangeImage.Navigation;
 using static PicView.UILogic.Loading.LoadContextMenus;
@@ -119,7 +117,7 @@ namespace PicView.UILogic.Loading
                     }));
                 }
 
-                await QuickLoad(args[1]).ConfigureAwait(false);
+                await ChangeImage.LoadPic.QuickLoad(args[1]).ConfigureAwait(false);
             }
         }
 

+ 2 - 1
PicView/Views/UserControls/Buttons/GoToPicButton.xaml.cs

@@ -1,4 +1,5 @@
 using PicView.Animations;
+using PicView.ChangeImage;
 using PicView.UILogic;
 using System.Globalization;
 using System.Threading.Tasks;
@@ -51,7 +52,7 @@ namespace PicView.Views.UserControls
                 x--;
                 x = x <= 0 ? 0 : x;
                 x = x >= Pics.Count ? Pics.Count - 1 : x;
-                await LoadPicAtIndexAsync(x).ConfigureAwait(false);
+                await LoadPic.LoadPicAtIndexAsync(x).ConfigureAwait(false);
                 await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(() =>
                 {
                     GetImageSettingsMenu.GoToPic.GoToPicBox.Text = (x + 1).ToString(CultureInfo.CurrentCulture);