Ejemplo n.º 1
0
 private static BitmapSource GetThumbnail(string path, ThumbnailOptions option = ThumbnailOptions.ThumbnailOnly)
 {
     return(WindowsThumbnailProvider.GetThumbnail(
                path,
                Constant.ThumbnailSize,
                Constant.ThumbnailSize,
                option));
 }
Ejemplo n.º 2
0
        private static ImageResult LoadInternal(string path, bool loadFullImage = false)
        {
            ImageSource image;
            ImageType   type = ImageType.Error;

            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    return(new ImageResult(ImageCache[Constant.ErrorIcon], ImageType.Error));
                }
                if (ImageCache.ContainsKey(path))
                {
                    return(new ImageResult(ImageCache[path], ImageType.Cache));
                }

                if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
                {
                    var imageSource = new BitmapImage(new Uri(path));
                    imageSource.Freeze();
                    return(new ImageResult(imageSource, ImageType.Data));
                }

                if (!Path.IsPathRooted(path))
                {
                    path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
                }

                if (Directory.Exists(path))
                {
                    /* Directories can also have thumbnails instead of shell icons.
                     * Generating thumbnails for a bunch of folders while scrolling through
                     * results from Everything makes a big impact on performance and
                     * Flow.Launcher responsibility.
                     * - Solution: just load the icon
                     */
                    type  = ImageType.Folder;
                    image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
                                                                  Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
                }
                else if (File.Exists(path))
                {
                    var extension = Path.GetExtension(path).ToLower();
                    if (ImageExtensions.Contains(extension))
                    {
                        type = ImageType.ImageFile;
                        if (loadFullImage)
                        {
                            image = LoadFullImage(path);
                        }
                        else
                        {
                            /* Although the documentation for GetImage on MSDN indicates that
                             * if a thumbnail is available it will return one, this has proved to not
                             * be the case in many situations while testing.
                             * - Solution: explicitly pass the ThumbnailOnly flag
                             */
                            image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
                                                                          Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly);
                        }
                    }
                    else
                    {
                        type  = ImageType.File;
                        image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
                                                                      Constant.ThumbnailSize, ThumbnailOptions.None);
                    }
                }
                else
                {
                    image = ImageCache[Constant.ErrorIcon];
                    path  = Constant.ErrorIcon;
                }

                if (type != ImageType.Error)
                {
                    image.Freeze();
                }
            }
            catch (System.Exception e)
            {
                Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
                type             = ImageType.Error;
                image            = ImageCache[Constant.ErrorIcon];
                ImageCache[path] = image;
            }
            return(new ImageResult(image, type));
        }