Example #1
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            lock (StoreLock)
            {
                var storedIcon = StoredIcons.FirstOrDefault(x => x.IconId == IconId && x.Size == Size);
                if (storedIcon != null)
                {
                    return(storedIcon.ImageSource);
                }

                using (
                    var extractor =
                        new IconExtractor(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86),
                                                       LibraryName)))
                    using (var icon = extractor.GetIconAt(IconId))
                    {
                        var info  = new IconInfo(icon);
                        var index = info.GetBestFitIconIndex(new Size(Size, Size));
                        using (var bestIcon = info.Images[index])
                        {
                            var imageSource = Imaging.CreateBitmapSourceFromHIcon(
                                bestIcon.Handle,
                                Int32Rect.Empty,
                                BitmapSizeOptions.FromEmptyOptions());
                            StoredIcons.Add(new StoredIcon {
                                IconId = IconId, ImageSource = imageSource, Size = Size
                            });
                            return(imageSource);
                        }
                    }
            }
        }
Example #2
0
        private static BitmapSource LoadIconFromFile(string filePath)
        {
            string ext = Path.GetExtension(filePath).ToLower();

            if (ext == ".exe" || ext == ".ico")
            {
                Icon icon = null;
                if (ext == ".exe")
                {
                    IconExtractor extractor = new IconExtractor(filePath);
                    icon = extractor.GetIconAt(0);
                    icon = IconHelper.GetBestFitIcon(icon, new System.Drawing.Size(48, 48));
                }
                else
                {
                    icon = new System.Drawing.Icon(filePath);
                    icon = IconHelper.GetBestFitIcon(icon, new System.Drawing.Size(48, 48));
                }

                BitmapSource bitmap = Imaging.CreateBitmapSourceFromHIcon(
                    icon.Handle,
                    new Int32Rect(0, 0, icon.Width, icon.Height),
                    BitmapSizeOptions.FromEmptyOptions()
                    );

                // Resize if needed
                int width  = bitmap.PixelWidth;
                int height = bitmap.PixelHeight;
                // Just incase the icon is having an identity crisis and isn't square
                int maxDimension = Math.Max(width, height);
                if (maxDimension > 48)
                {
                    bitmap = CreateResizedImage(bitmap, width * 48 / maxDimension, height * 48 / maxDimension);
                }

                return(bitmap);
            }
            else
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.UriSource   = new Uri(filePath);
                bitmap.EndInit();
                return(bitmap);
            }
        }