Esempio n. 1
0
 public static System.Windows.Media.ImageSource GetIconFromFilePath(string path, int width, int height, uint flags)
 {
     WinApi.ShFileInfo shInfo = new WinApi.ShFileInfo();
     WinApi.SHGetFileInfo(path, 0, ref shInfo, (uint)Marshal.SizeOf(shInfo), flags);
     System.Drawing.Icon entryIcon = System.Drawing.Icon.FromHandle(shInfo.hIcon);
     System.Windows.Media.ImageSource entryIconImageSource = Imaging.CreateBitmapSourceFromHIcon(
         entryIcon.Handle,
         Int32Rect.Empty,
         BitmapSizeOptions.FromWidthAndHeight(Convert.ToInt32(DpiManager.ConvertPixelsToWpfUnits(width)), Convert.ToInt32(DpiManager.ConvertPixelsToWpfUnits(height)))
         );
     return(entryIconImageSource);
 }
Esempio n. 2
0
        public static Color GetColorFromImage(string sourceImagePath)
        {
            var outputColor  = Color.Gray;
            var sourceBitmap = new Bitmap(1, 1);

            if (sourceImagePath == Environment.ExpandEnvironmentVariables(@"%windir%\Explorer.exe"))
            {
                outputColor = Color.FromArgb(255, 0, 130, 153);
            }
            else if (File.Exists(sourceImagePath) || Directory.Exists(sourceImagePath))
            {
                if (File.Exists(sourceImagePath))
                {
                    sourceBitmap = Bitmap.FromHicon(Icon.ExtractAssociatedIcon(sourceImagePath).Handle);
                }
                else if (Directory.Exists(sourceImagePath))
                {
                    var shinfo = new WinApi.ShFileInfo();
                    WinApi.SHGetFileInfo(sourceImagePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), 256);
                    sourceBitmap = Bitmap.FromHicon(shinfo.hIcon);
                }

                Color destColor;
                using (var destBitmap = new Bitmap(1, 1))
                    using (var g = Graphics.FromImage(destBitmap))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        g.DrawImage(sourceBitmap, 0, 0, 1, 1);

                        destColor = destBitmap.GetPixel(0, 0);
                    }
                outputColor = Color.FromArgb(255, destColor.R, destColor.G, destColor.B);
            }

            return(outputColor);
        }