Exemple #1
0
        public static ImageSource Load(string path, bool addToCache = true)
        {
            ImageSource image = null;

            if (string.IsNullOrEmpty(path))
            {
                path  = Path.Combine(Wox.ProgramPath, "Images", "app.png");
                image = new BitmapImage(new Uri(path));
                return(image);
            }
            Stopwatch.Debug($"Loading image path: {path}", () =>
            {
                if (addToCache)
                {
                    _cache.Add(path);
                }


                if (_imageSources.ContainsKey(path))
                {
                    image = _imageSources[path];
                }
                else
                {
                    string ext = Path.GetExtension(path).ToLower();

                    if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
                    {
                        image = new BitmapImage(new Uri(path));
                    }
                    else if (SelfExts.Contains(ext) && File.Exists(path))
                    {
                        image = GetIcon(path);
                    }
                    else if (!string.IsNullOrEmpty(path) && ImageExts.Contains(ext))
                    {
                        if (File.Exists(path))
                        {
                            image = new BitmapImage(new Uri(path));
                        }
                        else
                        {
                            path = Path.Combine(Wox.ProgramPath, "Images", Path.GetFileName(path));
                            if (File.Exists(path))
                            {
                                image = new BitmapImage(new Uri(path));
                            }
                            else
                            {
                                path  = Path.Combine(Wox.ProgramPath, "Images", "app.png");
                                image = new BitmapImage(new Uri(path));
                            }
                        }
                    }

                    if (image != null && addToCache)
                    {
                        if (!_imageSources.ContainsKey(path))
                        {
                            _imageSources[path] = image;
                        }
                    }
                }
            });
            return(image);
        }
Exemple #2
0
        public static ImageSource Load(string path)
        {
            ImageSource image;

            if (string.IsNullOrEmpty(path))
            {
                image = ImageSources[ErrorIcon];
                _cache.Add(ErrorIcon);
            }
            else if (ImageSources.ContainsKey(path))
            {
                image = ImageSources[path];
                _cache.Add(path);
            }
            else
            {
                if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
                {
                    image = new BitmapImage(new Uri(path));
                }
                else if (Path.IsPathRooted(path))
                {
                    if (Directory.Exists(path))
                    {
                        image = ShellIcon(path);
                    }
                    else if (File.Exists(path))
                    {
                        var externsion = Path.GetExtension(path).ToLower();
                        if (ImageExtions.Contains(externsion))
                        {
                            image = new BitmapImage(new Uri(path));
                        }
                        else
                        {
                            image = AssociatedIcon(path);
                        }
                    }
                    else
                    {
                        image = ImageSources[ErrorIcon];
                        path  = ErrorIcon;
                    }
                }
                else
                {
                    var defaultDirectoryPath = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
                    if (File.Exists(defaultDirectoryPath))
                    {
                        image = new BitmapImage(new Uri(defaultDirectoryPath));
                    }
                    else
                    {
                        image = ImageSources[ErrorIcon];
                        path  = ErrorIcon;
                    }
                }

                ImageSources[path] = image;
                _cache.Add(path);
            }
            return(image);
        }