示例#1
0
        internal static BitmapSource PathToIcon(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }
            if (!File.Exists(filePath))
            {
                int commaIndex = filePath.LastIndexOf(",", filePath.Length - 2, StringComparison.Ordinal);

                if (commaIndex >= 0)
                {
                    filePath = filePath.Substring(0, commaIndex);
                }
            }

            if (!File.Exists(filePath))
            {
                return(null);
            }
            using (Icon sysicon = Icon.ExtractAssociatedIcon(filePath))
            {
                return(sysicon == null
                    ? null
                    : Imaging.CreateBitmapSourceFromHIcon(sysicon.Handle, Int32Rect.Empty,
                                                          BitmapSizeOptions.FromEmptyOptions()));
            }
        }
        /// <summary>
        /// Gibt das zu der übergebenen Datei gehörige Icon zurück.
        /// </summary>
        public static BitmapSource GetIconForFile(string fileName)
        {
            var file = new System.IO.FileInfo(fileName);

            if (!file.Exists)
            {
                return(null);
            }

            IntPtr handle = default(IntPtr);

            handle = IconImage.ExtractAssociatedIcon(fileName).Handle;

            return(Imaging.CreateBitmapSourceFromHIcon(handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
        }
示例#3
0
        public IBitmap?GetFileIcon(string filePath)
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(null);
            }

            var icon = Icon.ExtractAssociatedIcon(filePath);

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

            var bitmap = icon.ToBitmap();

            return(new BitmapAdapter(bitmap));
        }
示例#4
0
        public void Execute(Window?owner)
        {
            // If there's no visible owner window, then this dialog should be centered on the screen.
            if (owner == null || owner.Visibility != Visibility.Visible)
            {
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                this.ShowInTaskbar         = true;
            }

            // We can't do "this.icon.Source = owner.Icon;" because that always gets a small (e.g., 16x16) icon.
            using (IconImage? appIcon = IconImage.ExtractAssociatedIcon(ApplicationInfo.ExecutableFile))
            {
                if (appIcon != null)
                {
                    // From comment at: http://www.infosysblogs.com/microsoft/2007/04/wpf_assigning_icon_to_image_co.html
                    BitmapSource bitmap = Imaging.CreateBitmapSourceFromHIcon(appIcon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    this.icon.Source = bitmap;
                }
            }

            this.Owner = owner;
            this.ShowDialog();
        }
示例#5
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value != null)
     {
         var          wIcon  = value as WindowIcon;
         MemoryStream stream = new MemoryStream();
         //if (icon.PlatformImpl is IconImpl impl)
         wIcon.Save(stream);
         stream.Position = 0;
         try
         {
             return(new ImageBrush(new Bitmap(stream)));
         }
         catch (ArgumentNullException ex)
         {
             try
             {
                 Icon icon = new Icon(stream);
                 System.Drawing.Bitmap bmp = icon.ToBitmap();
                 bmp.Save(stream, ImageFormat.Png);
                 return(new ImageBrush(new Bitmap(stream)));
             }
             catch (ArgumentException e)
             {
                 Icon icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetEntryAssembly().Location);
                 System.Drawing.Bitmap bmp = icon.ToBitmap();
                 Stream stream3            = new MemoryStream();
                 bmp.Save(stream3, ImageFormat.Png);
                 return(new ImageBrush(new Bitmap(stream3)));
             }
         }
     }
     else
     {
         return(null); //new ImageBrush();
     }
 }