Exemple #1
0
 public static extern IntPtr SHGetFileInfo(
     IntPtr pszPath,
     uint dwFileAttributes,
     ref ShFileInfo psfi,
     uint cbFileInfo,
     uint uFlags
     );
Exemple #2
0
        public static Icon GetFileIcon(string fileName, bool large)
        {
            ShFileInfo shinfo = new ShFileInfo();

            if (string.IsNullOrEmpty(fileName))
            {
                throw new Exception("File name cannot be empty.");
            }

            try
            {
                if (Win32.SHGetFileInfo(fileName, 0, out shinfo,
                                        (uint)Marshal.SizeOf(shinfo),
                                        Win32.ShgFiIcon |
                                        (large ? Win32.ShgFiLargeIcon : Win32.ShgFiSmallIcon)) == 0)
                {
                    return(null);
                }
                else
                {
                    return(Icon.FromHandle(shinfo.hIcon));
                }
            }
            catch
            {
                return(null);
            }
        }
Exemple #3
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ShFileInfo shFileInfo = new ShFileInfo();

            SHGetFileInfo((string)value, 0, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), ImageRetrievalFlags);

            return(Imaging.CreateBitmapSourceFromHBitmap(Icon.FromHandle(shFileInfo.iconHandle)
                                                         .ToBitmap()
                                                         .GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
        }
Exemple #4
0
 private static extern IntPtr ShGetFileInfo(
     // The path of the file
     string pszPath,
     // File attribute flags (unused)
     uint dwFileAttributes,
     // The ShFileInfo struct that should be populated with the return values.
     ref ShFileInfo psfi,
     // The size of the ShFileInfo structure.
     uint cbFileInfo,
     // Various flags (SHGetFileInfoAttributes) indicating what and how should be retrieved.
     uint uFlags
     );
        static ImageSource GetIcon(string fileName, uint flags)
        {
            var shinfo = new ShFileInfo();

            SHGetFileInfo(fileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | flags);

            using (var icon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
            {
                var img = Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
                                                              new Int32Rect(0, 0, icon.Width, icon.Height),
                                                              BitmapSizeOptions.FromEmptyOptions());
                return(img);
            }
        }
Exemple #6
0
        protected System.Drawing.Icon LoadImage(string path)
        {
            try
            {
                if (String.IsNullOrEmpty(path))
                {
                    return(null);
                }

                int      ix     = 0;
                string[] iconIx = path.Split(',');
                if (iconIx.Length > 1 && !String.IsNullOrEmpty(iconIx[1]))
                {
                    if (!int.TryParse(iconIx[1], out ix))
                    {
                        ix = 0;
                    }
                }

                path = Environment.ExpandEnvironmentVariables(iconIx[0]);

                if (!PathHelper.FileExists(path))
                {
                    return(null);
                }

                if (PathHelper.IsPathUNC(path))
                {
                    return(null);
                }

                if (Array.IndexOf(IconOwnExt, Path.GetExtension(path)) >= 0)
                {
                    return(Shell32.ExtractIconEx(path, ix));
                    //return System.Drawing.Icon.ExtractAssociatedIcon(path);
                }
                else
                {
                    //managed = false;
                    return(ShFileInfo.ExtractIcon(path, true));
                }
            }
            catch
            {; }

            return(null);
        }
Exemple #7
0
        /// <summary>
        /// Gets the icon and type name (using a Windows API call) of the file specified.
        /// </summary>
        /// <param name="path">The path of the file we need the icon of.</param>
        public static TypeAndIcon GetInformation(string path)
        {
            // Large icon
            ShFileInfo large = new ShFileInfo();
            ShGetFileInfoAttributes callLarge = ShGetFileInfoAttributes.Icon | ShGetFileInfoAttributes.UseFileAttributes |
                                                ShGetFileInfoAttributes.LargeIcon | ShGetFileInfoAttributes.SysIconIndex;

            ShGetFileInfo(path, (uint)System.IO.FileAttributes.Normal, ref large,
                          (uint)Marshal.SizeOf(large), (uint)callLarge);

            // Small icon
            ShFileInfo small = new ShFileInfo();
            ShGetFileInfoAttributes callSmall = ShGetFileInfoAttributes.Icon | ShGetFileInfoAttributes.UseFileAttributes |
                                                ShGetFileInfoAttributes.SmallIcon | ShGetFileInfoAttributes.SysIconIndex;

            ShGetFileInfo(path, (uint)System.IO.FileAttributes.Normal, ref small,
                          (uint)Marshal.SizeOf(small), (uint)callSmall);

            // Typename
            ShFileInfo typename = new ShFileInfo();
            ShGetFileInfoAttributes callTypename = ShGetFileInfoAttributes.UseFileAttributes | ShGetFileInfoAttributes.TypeName;

            ShGetFileInfo(path, (uint)System.IO.FileAttributes.Normal, ref typename,
                          (uint)Marshal.SizeOf(typename), (uint)callTypename);


            TypeAndIcon tai = new TypeAndIcon();

            tai.Filename = path;
            tai.Type     = typename.szTypeName;

            // Copy the retrieved icon into a local resource to disconnect it from the external one.
            if (large.hIcon != IntPtr.Zero && large.iIcon != 0)
            {
                tai.LargeIcon = (Icon)Icon.FromHandle(large.hIcon).Clone();
            }
            if (small.hIcon != IntPtr.Zero && small.iIcon != 0)
            {
                tai.SmallIcon = (Icon)Icon.FromHandle(small.hIcon).Clone();
            }

            DestroyIcon(large.hIcon);
            DestroyIcon(typename.hIcon);
            return(tai);
        }
Exemple #8
0
        private static int GetIconIndex(string filename, bool largeIcon)
        {
            var sfi      = new ShFileInfo();
            var sizeFlag = largeIcon
                       ? SHGetFileInfoConstants.LargeIcon
                       : SHGetFileInfoConstants.SmallIcon;

            Shell32.SHGetFileInfo(
                filename,
                0,
                ref sfi,
                (uint)Marshal.SizeOf(sfi),
                (uint)(SHGetFileInfoConstants.SysIconIndex |
                       sizeFlag |
                       SHGetFileInfoConstants.UseFileAttributes)
                );
            return(sfi.iIcon);
        }
        public static Icon GetFileIcon(string filePath, bool isLink)
        {
            string ext = Path.GetExtension(filePath).ToUpper();

            if (extensionList.ContainsKey(ext))
            {
                return((Icon)extensionList[ext]);
            }

            try
            {
                uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON;
                if (isLink)
                {
                    flags |= SHGFI_LINKOVERLAY;
                }

                ShFileInfo shellFileInfo = new ShFileInfo();

                SHGetFileInfo(filePath, FILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), (uint)flags);

                if (shellFileInfo.hIcon == IntPtr.Zero)
                {
                    return(null);
                }

                Icon icon = (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();

                DestroyIcon(shellFileInfo.hIcon);

                extensionList[ext] = icon;

                return(icon);
            }
            catch
            {
                extensionList[ext] = null;

                return(null);
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Process process = (Process)value;

            if (process == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            string processFilepath;

            try
            {
                processFilepath = process.MainModule.FileName;
            }
            catch (InvalidOperationException)
            {
                return(null);
            }
            catch (Win32Exception)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(processFilepath))
            {
                return(null);
            }

            ShFileInfo shFileInfo = new ShFileInfo();

            SHGetFileInfo(processFilepath, 0u, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), ImageRetrievalFlag);

            return(Imaging.CreateBitmapSourceFromHBitmap(Icon.FromHandle(shFileInfo.iconHandle)
                                                         .ToBitmap()
                                                         .GetHbitmap(),
                                                         IntPtr.Zero,
                                                         Int32Rect.Empty,
                                                         BitmapSizeOptions.FromEmptyOptions()));
        }
Exemple #11
0
        private static ImageSource CreateBitmapIcon(string fullName)
        {
            BitmapSource img = null;

            System.Drawing.Icon icon = null;
            Int32Rect           rect;

            try
            {
                icon = ShFileInfo.GetLargeIcon(fullName);
                rect = new Int32Rect(0, 0, icon.Width, icon.Height);
                img  = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, rect, BitmapSizeOptions.FromEmptyOptions());
                img.Freeze();
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            finally
            {
                icon?.Dispose();
            }
            return(img);
        }
Exemple #12
0
 public static extern IntPtr SHGetFileInfo(String pszPath, Int32 dwFileAttributes, ref ShFileInfo psfi, Int32 cbSizeFileInfo, Int32 uFlags);
 private static extern System.IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref ShFileInfo psfi, uint cbFileInfo, uint uFlags);
 private static extern IntPtr SHGetFileInfo(string filePath, uint fileAttributes, ref ShFileInfo shFileInfoPointer, uint imagePointerSize, uint flags);
Exemple #15
0
 // ---------------------------------------------------------------------------------------------------------------------
 public static IntPtr ShGetFileInfoVisible(string pszPath, uint dwFileAttributes, ref ShFileInfo psfi,
                                           uint cbSizeFileInfo, uint uFlags)
 {
     return(NativeMethods.SHGetFileInfo(pszPath, dwFileAttributes, ref psfi, cbSizeFileInfo, uFlags));
 }
Exemple #16
0
 public static extern IntPtr SHGetFileInfo(String pszPath, Int32 dwFileAttributes, ref ShFileInfo psfi, Int32 cbSizeFileInfo, Int32 uFlags);