Esempio n. 1
0
        /// <summary>
        /// Loads image at specified index
        /// </summary>
        /// <param name="index">The index of file to load from Pics</param>
        internal static async Task LoadPicAt(int index)
        {
            Preloader.PreloadValue preloadValue;
            // Error checking to fix rare cases of crashing
            if (Pics.Count < index)
            {
                preloadValue = await PicErrorFix(index).ConfigureAwait(true);

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

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

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

                if (!GalleryFunctions.IsOpen)
                {
                    // Show a thumbnail while loading
                    var thumb = GetThumb(index);
                    await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                    {
                        // Set loading from translation service
                        SetLoadingString();

                        // Don't allow image size to stretch the whole screen
                        if (xWidth == 0)
                        {
                            ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
                            ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
                        }

                        if (thumb != null)
                        {
                            ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
                        }
                    }));
                }

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

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

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

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

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

                    ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
                }

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

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

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

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

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

            // Add recent files, except when browing archive
            if (string.IsNullOrWhiteSpace(TempZipFile))
            {
                RecentFiles.Add(Pics[index]);
            }
        }
Esempio n. 2
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
        }
Esempio n. 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)
        {
            FolderIndex = index;

            // 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;
                }
            }

            /// Retrieve from preloader if available
            /// if not, it will be null
            bitmapSource = Preloader.Get(Pics[index]);

            // Initate loading behavior, if needed
            if (bitmapSource == null)
            {
                // 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.PicGallery != 2)
                    {
                        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;
                        }));
                    }
                }

                // 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;
            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);
            }

            if (string.IsNullOrWhiteSpace(TempZipFile))
            {
                RecentFiles.Add(Pics[index]);
            }
        }