Example #1
0
        /// <summary>
        /// Loads a picture from a given file path and does extra error checking
        /// </summary>
        /// <param name="path"></param>
        internal static async void Pic(string path)
        {
            // Set Loading
            SetLoadingString();

            // Handle if from web
            if (!File.Exists(path))
            {
                if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
                {
                    LoadFromWeb.PicWeb(path);
                    return;
                }
                else if (Directory.Exists(path))
                {
                    ChangeFolder(true);
                    await GetValues(path).ConfigureAwait(true);
                }
                else
                {
                    Unload();
                    return;
                }
            }

            // If count not correct or just started, get values
            if (Pics.Count <= FolderIndex || FolderIndex < 0 || FreshStartup)
            {
                await GetValues(path).ConfigureAwait(true);
            }
            // 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(true);
            }

            FolderIndex = Pics.IndexOf(path);

            if (!FreshStartup)
            {
                Preloader.Clear();
            }

#if DEBUG
            if (FreshStartup)
            {
                Trace.WriteLine("Pic(string path) entering Pic(int x)");
            }
#endif
            if (FolderIndex != -1) // if it is -1, it means it being extracted and need to wait for it instead
            {
                // Navigate to picture using obtained index
                Pic(FolderIndex);
            }


            // Load new gallery values, if changing folder
            if (GetPicGallery != null && Properties.Settings.Default.PicGallery == 2)
            {
                if (GetPicGallery.Container.Children.Count == 0)
                {
                    await GalleryLoad.Load().ConfigureAwait(false);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Goes to next, previous, first or last file in folder
        /// </summary>
        /// <param name="next">Whether it's forward or not</param>
        /// <param name="end">Whether to go to last or first,
        /// depending on the next value</param>
        internal static void Pic(bool next = true, bool end = false)
        {
            // Exit if not intended to change picture
            if (!CanNavigate)
            {
                return;
            }

            // exit if browsing PicGallery
            if (GetPicGallery != null)
            {
                if (Properties.Settings.Default.PicGallery == 1)
                {
                    if (GalleryFunctions.IsOpen)
                    {
                        return;
                    }
                }
            }

            // Make backup
            var indexBackup = FolderIndex;

            if (!end) // Go to next or previous
            {
                if (next)
                {
                    // loop next
                    if (Properties.Settings.Default.Looping || Slideshow.SlideTimer != null && Slideshow.SlideTimer.Enabled)
                    {
                        FolderIndex = FolderIndex == Pics.Count - 1 ? 0 : FolderIndex + 1;
                    }
                    else
                    {
                        // Go to next if able
                        if (FolderIndex + 1 == Pics.Count)
                        {
                            return;
                        }

                        FolderIndex++;
                    }
                    Reverse = false;
                }
                else
                {
                    // Loop prev
                    if (Properties.Settings.Default.Looping || Slideshow.SlideTimer != null && Slideshow.SlideTimer.Enabled)
                    {
                        FolderIndex = FolderIndex == 0 ? Pics.Count - 1 : FolderIndex - 1;
                    }
                    else
                    {
                        // Go to prev if able
                        if (FolderIndex - 1 < 0)
                        {
                            return;
                        }

                        FolderIndex--;
                    }
                    Reverse = true;
                }
            }
            else // Go to first or last
            {
                FolderIndex = next ? Pics.Count - 1 : 0;
                indexBackup = FolderIndex;

                // Reset preloader values to prevent errors
                if (Pics.Count > 20)
                {
                    Preloader.Clear();
                }
            }

            // Go to the image!
            Pic(FolderIndex);

            // Update PicGallery selected item, if needed
            if (GalleryFunctions.IsOpen)
            {
                if (GetPicGallery.Container.Children.Count > FolderIndex && GetPicGallery.Container.Children.Count > indexBackup)
                {
                    if (indexBackup != FolderIndex)
                    {
                        GalleryFunctions.SetUnselected(indexBackup);
                    }

                    GalleryFunctions.SetSelected(FolderIndex);
                    GalleryScroll.ScrollTo();
                }
                else
                {
                    // TODO Find way to get PicGalleryItem an alternative way...
                }
            }

            CloseToolTipMessage();
        }
Example #3
0
        /// <summary>
        /// Loads image at specified index
        /// </summary>
        /// <param name="index">The index of file to load from Pics</param>
        internal static async void Pic(int index)
        {
#if DEBUG
            var stopWatch = new Stopwatch();
            stopWatch.Start();
#endif
            // Declare variable to be used to set image source
            BitmapSource bitmapSource;

            // Error checking to fix rare cases of crashing
            if (Pics.Count < index)
            {
                bitmapSource = await PicErrorFix(index).ConfigureAwait(true);

                if (bitmapSource == null)
                {
                    /// Try to recover
                    /// TODO needs testing
                    Reload(true);
                    return;
                }
            }
            else if (File.Exists(Pics[index])) // Checking if file exists fixes rare crashes
            {
                /// Retrieve from preloader if available
                /// if not, it will be null
                bitmapSource = Preloader.Get(Pics[index]);
            }
            else
            {
                /// Try to reload from backup if file does not exist
                /// TODO needs testing
                Reload(true);
                return;
            }

            // Initate loading behavior, if needed
            if (bitmapSource == null)
            {
                // Set loading from translation service
                SetLoadingString();

                // Show a thumbnail while loading
                var thumb = GetThumb(index, true);
                if (thumb != null && Properties.Settings.Default.PicGallery != 2)
                {
                    // Don't allow image size to stretch the whole screen
                    if (xWidth == 0)
                    {
                        ConfigureWindows.GetMainWindow.MainImage.Width  = ConfigureWindows.GetMainWindow.MinWidth;
                        ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.MinHeight;
                    }
                    else
                    {
                        ConfigureWindows.GetMainWindow.MainImage.Width  = xWidth;
                        ConfigureWindows.GetMainWindow.MainImage.Height = xHeight;
                    }

                    ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
                }

                // Dissallow changing image while loading
                CanNavigate = false;

                // Get it!
                await Preloader.Add(Pics[index]).ConfigureAwait(true);

                // Retry
                bitmapSource = Preloader.Get(Pics[index]);

                if (bitmapSource == null)
                {
                    // If pic is still null, image can't be rendered
                    bitmapSource = ImageDecoder.ImageErrorMessage();
                }
            }

            // Need to put UI change in dispatcher to fix slideshow bug
            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, (Action)(() =>
            {
                // Scroll to top if scroll enabled
                if (IsScrollEnabled)
                {
                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
                }

                // Reset transforms if needed
                if (UILogic.TransformImage.Rotation.Flipped || UILogic.TransformImage.Rotation.Rotateint != 0)
                {
                    UILogic.TransformImage.Rotation.Flipped = false;
                    UILogic.TransformImage.Rotation.Rotateint = 0;
                    GetImageSettingsMenu.FlipButton.TheButton.IsChecked = false;

                    ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
                }

                ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
                FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
                SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index);
            }));

            // Update values
            CanNavigate  = true;
            FolderIndex  = index;
            FreshStartup = false;

            if (ConfigureWindows.GetImageInfoWindow != null)
            {
                if (ConfigureWindows.GetImageInfoWindow.IsVisible)
                {
                    ConfigureWindows.GetImageInfoWindow.UpdateValues();
                }
            }

            if (Pics.Count > 1)
            {
                Taskbar.Progress(index, Pics.Count);

                // Preload images \\
                await Preloader.PreLoad(index).ConfigureAwait(false);
            }

            RecentFiles.Add(Pics[index]);

#if DEBUG
            stopWatch.Stop();
            var s = $"Pic(); executed in {stopWatch.Elapsed.TotalMilliseconds} milliseconds";
            Trace.WriteLine(s);
#endif
        }
Example #4
0
        /// <summary>
        /// Loads a picture from a given file path and does extra error checking
        /// </summary>
        /// <param name="path"></param>
        internal static async void Pic(string path)
        {
            // Set Loading
            SetLoadingString();

            // Handle if from web
            if (!File.Exists(path))
            {
                if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
                {
                    LoadFromWeb.PicWeb(path);
                    return;
                }
                else if (Directory.Exists(path))
                {
                    ChangeFolder(true);
                    await GetValues(path).ConfigureAwait(true);
                }
                else
                {
                    Unload();
                    return;
                }
            }

            // If count not correct or just started, get values
            if (Pics.Count <= FolderIndex || FolderIndex < 0 || FreshStartup)
            {
                await GetValues(path).ConfigureAwait(true);
            }
            // 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(true);
            }

            FolderIndex = Pics.IndexOf(path);

            // Fix large archive extraction error
            if (FolderIndex == -1)
            {
                var recovery = await RecoverFailedArchiveAsync().ConfigureAwait(true);

                if (!recovery)
                {
                    Reload(true);
                    return;
                }
                else
                {
                    ConfigureWindows.GetMainWindow.TitleText.Text    = Application.Current.Resources["Unzipping"] as string;
                    ConfigureWindows.GetMainWindow.TitleText.ToolTip = ConfigureWindows.GetMainWindow.TitleText.Text;
                    FolderIndex = 0;
                }
                ConfigureWindows.GetMainWindow.Focus();
            }

            if (!FreshStartup)
            {
                Preloader.Clear();
            }

#if DEBUG
            if (FreshStartup)
            {
                Trace.WriteLine("Pic(string path) entering Pic(int x)");
            }
#endif

            // Navigate to picture using obtained index
            Pic(FolderIndex);

            // Load new gallery values, if changing folder
            if (GetPicGallery != null && Properties.Settings.Default.PicGallery == 2)
            {
                if (GetPicGallery.Container.Children.Count == 0)
                {
                    await GalleryLoad.Load().ConfigureAwait(false);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Loads image at specified index
        /// </summary>
        /// <param name="index">The index of file to load from Pics</param>
        internal static async void Pic(int index)
        {
#if DEBUG
            var stopWatch = new Stopwatch();
            stopWatch.Start();
#endif
            // Declare variable to be used to set image source
            BitmapSource bitmapSource;

            // Error checking to fix rare cases of crashing
            if (Pics.Count < index)
            {
                bitmapSource = await PicErrorFix(index).ConfigureAwait(true);

                if (bitmapSource == null)
                {
                    /// Try to recover
                    /// TODO needs testing
                    Reload(true);
                    return;
                }
            }
            else if (File.Exists(Pics[index])) // Checking if file exists fixes rare crashes
            {
                /// Use the Load() function load image from memory if available
                /// if not, it will be null
                bitmapSource = Preloader.Load(Pics[index]);
            }
            else
            {
                /// Try to reload from backup if file does not exist
                /// TODO needs testing
                Reload(true);
                return;
            }

            // Initate loading behavior, if needed
            if (bitmapSource == null)
            {
                // Set loading from translation service
                SetLoadingString();

                // Show a thumbnail while loading
                var thumb = GetThumb(index, true);
                if (thumb != null && Properties.Settings.Default.PicGallery != 2)
                {
                    // Don't allow image size to stretch the whole screen
                    if (xWidth == 0)
                    {
                        var size = ImageDecoder.ImageSize(Pics[index]);
                        if (size.HasValue)
                        {
                            FitImage(size.Value.Width, size.Value.Height);
                        }
                        else
                        {
                            LoadWindows.GetMainWindow.MainImage.Width  = LoadWindows.GetMainWindow.MinWidth;
                            LoadWindows.GetMainWindow.MainImage.Height = LoadWindows.GetMainWindow.MinHeight;
                        }
                    }
                    else
                    {
                        LoadWindows.GetMainWindow.MainImage.Width  = xWidth;
                        LoadWindows.GetMainWindow.MainImage.Height = xHeight;
                    }

                    LoadWindows.GetMainWindow.MainImage.Source = thumb;
                }

                // Dissallow changing image while loading
                CanNavigate = false;

                // Get it!
                await Preloader.Add(Pics[index]).ConfigureAwait(true);

                // Retry
                bitmapSource = Preloader.Load(Pics[index]);

                if (bitmapSource == null)
                {
                    // Attempt to fix it
                    bitmapSource = await PicErrorFix(index).ConfigureAwait(true);

                    // If pic is still null, image can't be rendered
                    if (bitmapSource == null)
                    {
                        // Clean up
                        Pics.RemoveAt(index);
                        Preloader.Remove(index);

                        // Sync with gallery, if needed
                        if (GetPicGallery != null)
                        {
                            if (GetPicGallery.grid.Children.Count > index)
                            {
                                GetPicGallery.grid.Children.RemoveAt(index);
                            }
                        }

                        // Check if images still exists
                        if (Pics.Count == 0)
                        {
                            Unload();
                            return;
                        }

                        /// Retry
                        /// TODO needs testing
                        CanNavigate = true;
                        Pic();
                        return;
                    }
                }
            }

            // Reset transforms if needed
            if (UILogic.TransformImage.Rotation.Flipped || UILogic.TransformImage.Rotation.Rotateint != 0)
            {
                UILogic.TransformImage.Rotation.Flipped             = false;
                UILogic.TransformImage.Rotation.Rotateint           = 0;
                GetImageSettingsMenu.FlipButton.TheButton.IsChecked = false;

                LoadWindows.GetMainWindow.MainImage.LayoutTransform = null;
            }

            // Show the image! :)
            LoadWindows.GetMainWindow.MainImage.Source = bitmapSource;
            FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
            SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index);

            // Scroll to top if scroll enabled
            if (IsScrollEnabled)
            {
                LoadWindows.GetMainWindow.Scroller.ScrollToTop();
            }

            // Update values
            CanNavigate  = true;
            FolderIndex  = index;
            FreshStartup = false;

            if (LoadWindows.GetImageInfoWindow != null)
            {
                if (LoadWindows.GetImageInfoWindow.IsVisible)
                {
                    LoadWindows.GetImageInfoWindow.UpdateValues();
                }
            }

            if (Pics.Count > 1)
            {
                Taskbar.Progress(index, Pics.Count);

                // Preload images \\
                await Preloader.PreLoad(index).ConfigureAwait(false);
            }

            RecentFiles.Add(Pics[index]);

#if DEBUG
            stopWatch.Stop();
            var s = $"Pic(); executed in {stopWatch.Elapsed.TotalMilliseconds} milliseconds";
            Trace.WriteLine(s);
#endif
        }
Example #6
0
        /// <summary>
        /// Loads image at specified index
        /// </summary>
        /// <param name="index">The index of file to load from Pics</param>
        internal static async void Pic(int index)
        {
            Preloader.PreloadValue preloadValue;
            // Error checking to fix rare cases of crashing
            if (Pics.Count < index)
            {
                preloadValue = await PicErrorFix(index).ConfigureAwait(true);

                if (preloadValue == null)
                {
                    /// Try to recover
                    /// TODO needs testing
                    Reload(true);
                    return;
                }
            }

            FolderIndex  = index;
            preloadValue = Preloader.Get(Pics[index]);

            // Initate loading behavior, if needed
            if (preloadValue == null || preloadValue.isLoading)
            {
                // Dissallow changing image while loading
                CanNavigate = false;

                if (!GalleryFunctions.IsOpen)
                {
                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                    {
                        // Set loading from translation service
                        SetLoadingString();
                    }));

                    // Show a thumbnail while loading
                    var thumb = GetThumb(index);
                    if (thumb != null && Properties.Settings.Default.FullscreenGallery == false)
                    {
                        await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                        {
                            // Don't allow image size to stretch the whole screen
                            if (xWidth == 0)
                            {
                                ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.MinWidth;
                                ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.MinHeight;
                            }
                            else
                            {
                                ConfigureWindows.GetMainWindow.MainImage.Width = xWidth;
                                ConfigureWindows.GetMainWindow.MainImage.Height = xHeight;
                            }

                            ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
                        }));
                    }
                }

                if (preloadValue == null) // Error correctiom
                {
                    await Preloader.Add(Pics[index]).ConfigureAwait(true);

                    preloadValue = Preloader.Get(Pics[index]);
                }
                while (preloadValue.isLoading)
                {
                    // Wait for finnished result
                    await Task.Delay(2).ConfigureAwait(true);
                }

                // Retry
                preloadValue = Preloader.Get(Pics[index]);

                if (preloadValue == null)
                {
                    await Preloader.Add(Pics[index]).ConfigureAwait(true);

                    preloadValue = Preloader.Get(Pics[index]);
                }

                // Check if works, if not show error message
                if (preloadValue == null)
                {
                    preloadValue = new Preloader.PreloadValue(ImageDecoder.ImageErrorMessage(), false);
                }
                else if (preloadValue.bitmapSource == null)
                {
                    preloadValue.bitmapSource = ImageDecoder.ImageErrorMessage();
                }
            }

            // Need to put UI change in dispatcher to fix slideshow bug
            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, (Action)(() =>
            {
                // Scroll to top if scroll enabled
                if (IsScrollEnabled)
                {
                    ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
                }

                // Reset transforms if needed
                if (UILogic.TransformImage.Rotation.Flipped || UILogic.TransformImage.Rotation.Rotateint != 0)
                {
                    UILogic.TransformImage.Rotation.Flipped = false;
                    UILogic.TransformImage.Rotation.Rotateint = 0;
                    GetImageSettingsMenu.FlipButton.TheButton.IsChecked = false;

                    ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
                }

                ConfigureWindows.GetMainWindow.MainImage.Source = preloadValue.bitmapSource;
                FitImage(preloadValue.bitmapSource.PixelWidth, preloadValue.bitmapSource.PixelHeight);
                SetTitleString(preloadValue.bitmapSource.PixelWidth, preloadValue.bitmapSource.PixelHeight, index);
            }));

            // Update values
            CanNavigate  = true;
            FreshStartup = false;

            if (ConfigureWindows.GetImageInfoWindow != null)
            {
                if (ConfigureWindows.GetImageInfoWindow.IsVisible)
                {
                    ConfigureWindows.GetImageInfoWindow.UpdateValues();
                }
            }

            if (Pics.Count > 1)
            {
                Taskbar.Progress(index, Pics.Count);

                // Preload images \\
                await Preloader.PreLoad(index).ConfigureAwait(false);
            }

            // Add recent files, except when browing archive
            if (string.IsNullOrWhiteSpace(TempZipFile))
            {
                RecentFiles.Add(Pics[index]);
            }
        }