Beispiel #1
0
        public BitmapSource GetImageForApplicationName(string appFullPath)
        {
            const int iconSize = 16;

            // normalize case and trim quotes
            appFullPath = appFullPath.ToLowerInvariant().Trim('\"', '\'');

            // expand environment variables
            int i = 0;

            while ((i = appFullPath.IndexOf('%', i)) >= 0)
            {
                int j = appFullPath.IndexOf('%', i + 1);

                // uh-oh - registry may be corrupt
                if (j <= i)
                {
                    return(null);
                }

                var envVar      = appFullPath.Substring(i + 1, j - 1);
                var envVarValue = Environment.GetEnvironmentVariable(envVar);
                appFullPath = appFullPath.Replace($"%{envVar}%", envVarValue);
            }

            // check cache
            if (AppIcons.TryGetValue(appFullPath, out BitmapCollection <BitmapSource> bitmaps))
            {
                if (bitmaps.TryGetValue(iconSize, out BitmapSource cached))
                {
                    return(cached);
                }
            }

            // load from app (dll or exe)
            var bitmapImage = GetBitmapImageFromApp(appFullPath, small: true);

            if (bitmapImage == null)
            {
                return(null);
            }

            if (bitmaps == null)
            {
                bitmaps = new BitmapCollection <BitmapSource>();
                AppIcons.Add(appFullPath, bitmaps);
            }

            // store in cache
            bitmaps.Add(bitmapImage, iconSize);

            return(bitmapImage);
        }
Beispiel #2
0
        /// <summary>
        /// Loads a known icon from the app resources
        /// </summary>
        private BitmapCollection <BitmapSource> LoadKnownIcon(string id)
        {
            using (var stream = Application.GetResourceStream(new Uri($"pack://application:,,,/MOPE;component/icons/{id}.ico")).Stream)
            {
                // load 16, 32, and 256 sizes
                var collection = new BitmapCollection <BitmapSource>();

                collection.Add(GetBitmapSourceFromIconStream(stream, 16), 16);
                stream.Seek(0, SeekOrigin.Begin);

                collection.Add(GetBitmapSourceFromIconStream(stream, 32), 32);
                stream.Seek(0, SeekOrigin.Begin);

                collection.Add(GetBitmapSourceFromIconStream(stream, 64), 64);

                return(collection);
            }
        }