Пример #1
0
        public static BitmapSource GetWin32Icon(string fullName, IconSize size)
        {
            try
            {
                BitmapSource bmp;

                switch (size)
                {
                case IconSize.ExtraSmall:
                case IconSize.Small:
                    bmp = IconExtractor.GetFileIcon(fullName, size).ToBitmap().ToBitmapSource();
                    break;

                case IconSize.Large:
                    bmp = jumboIconList.Icon(jumboIconList.IconIndex(fullName, Directory.Exists(fullName)))
                          .ToBitmap().Shrink(new System.Drawing.Size(90, 90)).ToBitmapSource();
                    break;

                default:
                    bmp = jumboIconList.Icon(jumboIconList.IconIndex(fullName, Directory.Exists(fullName)))
                          .ToBitmap().ToBitmapSource();
                    break;
                }

                if (bmp != null)
                {
                    bmp.Freeze();
                }

                return(bmp);
            }
            catch { }

            return(null);
        }
Пример #2
0
        //private static async Task<BitmapSource> GetCachedAsync(string uri, Func<Task<BitmapSource>> getIcon)
        //{
        //	BitmapSource data;

        //	if (_cache.TryGetValue(uri, out data))
        //		return data;

        //	data = await getIcon();

        //	try { _cache.Add(uri, data); }
        //	catch (ArgumentException)
        //	{
        //		// Since this method can be called asynchronously, there is a chance
        //		// that another call already created this key.
        //	}

        //	return data;
        //}

        public static async Task <BitmapSource> GetIcon(string fullName, IconSize size)
        {
            fullName = await IOHelpers.Normalize(fullName, false);

            return(await Task.Factory.StartNew <BitmapSource>(() =>
            {
                if (fullName == string.Empty)
                {
                    BitmapSource source = GetCached(fullName, size, GetDefaultComputerIcon);

                    if (source != null && source.CanFreeze)
                    {
                        source.Freeze();
                    }

                    return source;
                }
                else
                {
                    bool isFile = File.Exists(fullName);

                    string cacheKey = fullName;

                    string extension = null;

                    if (isFile)
                    {
                        extension = Path.GetExtension(fullName).ToLower();

                        // Icons, executables, links, and cursors have unique icons.
                        if (IsUniqueIcon(extension))
                        {
                            cacheKey = fullName;
                        }
                        else if (size == IconSize.ExtraSmall || !IsImage(extension))
                        {
                            cacheKey = extension;
                        }
                        else
                        {
                            cacheKey = fullName;
                        }
                    }

                    BitmapSource source = GetCached(cacheKey, size, () =>
                    {
                        BitmapSource img = !isFile || !IsImage(extension) ?
                                           IconExtractor.GetWin32Icon(fullName, size) : IconExtractor.GetImagePreview(fullName, extension, size);

                        if (img != null)
                        {
                            return img;
                        }

                        if (isFile)
                        {
                            return GetCached("<Fil>", size, GetDefaultFileIcon);
                        }
                        else
                        {
                            return GetCached("<Dir>", size, GetDefaultFolderIcon);
                        }
                    });

                    if (source != null && source.CanFreeze)
                    {
                        source.Freeze();
                    }

                    return source;
                }
            }));
        }