Example #1
0
        /// <summary>
        /// Loads image based on overloaded int.
        /// </summary>
        /// <param name="x">The index of file to load from Pics</param>
        internal static async void Pic(int x)
        {
            BitmapSource pic;

            // Additional error checking
            if (Pics.Count <= x)
            {
                if (x == 0)
                {
                    var recovery = await RecoverFailedArchiveAsync().ConfigureAwait(true);

                    if (!recovery)
                    {
                        ToolTipStyle("Archive could not be processed");
                        Reload(true);
                        return;
                    }
                }

                // Untested code
                pic = await PicErrorFix(x).ConfigureAwait(true);

                if (pic == null)
                {
                    Reload(true);
                    return;
                }
            }
            if (x < 0)
            {
                pic = await PicErrorFix(x).ConfigureAwait(true);
            }
            //if (!canNavigate)
            //{
            //    Reload(true);
            //    return;
            //}
            else
            {
                /// Add "pic" as local variable used for the image.
                /// Use the Load() function load image from memory if available
                /// if not, it will be null
                pic = Preloader.Load(Pics[x]);
            }


            if (pic == null)
            {
                mainWindow.Title       = Loading;
                mainWindow.Bar.Text    = Loading;
                mainWindow.Bar.ToolTip = Loading;

                TryZoomFit(Pics[x]);

                var thumb = GetThumb();

                if (thumb != null)
                {
                    mainWindow.img.Source = thumb;
                }

                // Dissallow changing image while loading
                canNavigate = false;

                if (freshStartup)
                {
                    // Load new value manually
                    await Task.Run(() => pic = RenderToBitmapSource(Pics[x])).ConfigureAwait(true);
                }
                else
                {
                    do
                    {
                        // Try again while loading?
                        await Task.Delay(20).ConfigureAwait(true);

                        if (x < Pics.Count)
                        {
                            pic = Preloader.Load(Pics[x]);
                        }
                    } while (Preloader.IsLoading);
                }

                // If pic is still null, image can't be rendered
                if (pic == null)
                {
                    // Attempt to load new image
                    pic = await PicErrorFix(x).ConfigureAwait(true);

                    if (pic == null)
                    {
                        if (Pics.Count <= 1)
                        {
                            Unload();
                            return;
                        }

                        DisplayBrokenImage();
                        canNavigate = true;
                        return;
                    }
                }
            }

            // Clear unsupported image window, if shown
            if (mainWindow.img.Source == null && !freshStartup)
            {
                if (mainWindow.topLayer.Children.Count > 0)
                {
                    mainWindow.topLayer.Children.Clear();
                }
            }

            // Show the image! :)
            mainWindow.img.Source = pic;

            ZoomFit(pic.PixelWidth, pic.PixelHeight);

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

            /// TODO Make it staying flipped a user preference
            //// Prevent picture from being flipped if previous is
            //if (Flipped)
            //    Flip();

            // Update values
            canNavigate = true;
            SetTitleString(pic.PixelWidth, pic.PixelHeight, x);
            FolderIndex = x;
            AjaxLoadingEnd();

            if (Pics.Count > 0)
            {
                Progress(x, Pics.Count);

                // Preload images \\
                if (Preloader.StartPreload())
                {
                    Preloader.Add(pic, Pics[FolderIndex]);
                    await Preloader.PreLoad(x).ConfigureAwait(false);
                }
            }

            if (!freshStartup)
            {
                RecentFiles.Add(Pics[x]);
            }

            freshStartup = false;
        }