Exemple #1
0
        /// <summary>
        /// Get the image for the specified file type.
        /// You can provide the full file name, for example doc1.docx
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>Image or null. The size of the image = size of the icon</returns>
        private static Image FileTypeImage(string fileName)
        {
            Image      result = null;
            Icon       icon   = null;
            IconFlags  flags;
            IntPtr     hIcon = IntPtr.Zero;
            IntPtr     shellInfo;
            IconSize   size;
            IImageList iImageList = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                SHFILEINFO fileInfo = new SHFILEINFO();
                fileName = FileSearch.FullPath(fileName);

                if (FileOperations.FileOrFolderExists(fileName))
                {
                    fileInfo.dwAttributes = (uint)File.GetAttributes(fileName);
                }

                //first try to query the icon location and get the icon from there
                flags     = IconFlags.LargeIcon | IconFlags.IconLocation | IconFlags.UseFileAttributes;
                shellInfo = NativeMethods.SHGetFileInfo(fileName, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), flags);
                if (shellInfo == (IntPtr)1)
                {
                    //we have the result, try to parse it
                    if (FileOperations.FileIsIcon(fileInfo.szDisplayName))
                    {
                        icon   = new Icon(fileInfo.szDisplayName);
                        result = ConvertIconToBitmap(icon);
                        icon.Dispose();
                        return(result);
                    }
                    else
                    if (FileOperations.FileIsExe(fileName))
                    {
                        result = ExtractIconFromExecutable(fileInfo.szDisplayName, (int)fileInfo.iIcon);
                    }
                }

                if (result != null)
                {
                    return(result);
                }

                //if we have no result: continue to search using system image list
                flags = IconFlags.LargeIcon | IconFlags.SysIconIndex;
                if (!FileOperations.FileOrFolderExists(fileName))
                {
                    flags = flags | IconFlags.UseFileAttributes;
                }
                shellInfo = NativeMethods.SHGetFileInfo(fileName, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), flags);

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


                size = IconSize.ExtraLarge;

                if (InteropHelper.RunningOnXP)
                {
                    Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                    NativeMethods.SHGetImageList(
                        (int)size,
                        ref iidImageList,
                        ref iImageList
                        );
                    // the image list handle is the IUnknown pointer, but
                    // using Marshal.GetIUnknownForObject doesn't return
                    // the right value.  It really doesn't hurt to make
                    // a second call to get the handle:
                    NativeMethods.SHGetImageListHandle((int)size, ref iidImageList, ref shellInfo);
                }

                if (iImageList == null)
                {
                    hIcon = NativeMethods.ImageList_GetIcon(shellInfo, (int)fileInfo.iIcon, NativeMethods.ILD_TRANSPARENT);
                }
                else
                {
                    iImageList.GetIcon((int)fileInfo.iIcon, (int)NativeMethods.ILD_TRANSPARENT, ref hIcon);
                }

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

                icon = Icon.FromHandle(hIcon);
                //do not destroy icon from the system image list
                //NativeMethods.DestroyIcon(hIcon);
                result = ConvertIconToBitmap(icon);
            }


            if (icon != null)
            {
                icon.Dispose();
            }

            if (result != null)
            {
                result = BitmapPainter.ResizeBitmap(result, imageSize, imageSize, true);
            }

            return(result);
        }
Exemple #2
0
        private static Image FileNameImageInternal(string fileName)
        {
            Image result = null;
            Icon  icon   = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                fileName = FileOperations.StripFileName(fileName);
                //if we have only name of the file without path
                //try to find it in the search path of Windows
                fileName = FileSearch.FullPath(fileName);

                if (!FileOperations.FileOrFolderExists(fileName))
                {
                    result = FileTypeImage(fileName);
                    return(result);
                }

                //if this is a link to executable with original
                //executable icon
                if (FileOperations.FileIsLink(fileName))
                {
                    result = TryExtractImageFromShellLink(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (FileOperations.FileIsImage(fileName))
                {
                    try
                    {
                        result = Image.FromFile(fileName);
                        result = BitmapPainter.ResizeBitmap(result, imageSize, imageSize, true);
                    }
                    catch
                    {
                        result = null;
                    }
                    if (result != null)
                    {
                        return(result);
                    }
                }

                // 1. The file is icon
                // 2. The file is exe or dll
                // 3. The file is a shell link .lnk
                // 4. Other file types
                if (FileOperations.FileIsIcon(fileName))
                {
                    //Simply load icon from file
                    icon = new Icon(fileName);
                }
                else
                //Extract icon from exe or dll directly
                if (FileOperations.FileIsExe(fileName))
                {
                    return(ExtractIconFromExecutable(fileName));
                }
                else
                {
                    result = FileTypeImage(fileName);
                    return(result);
                }

                result = ConvertIconToBitmap(icon);
            }

            if (icon != null)
            {
                icon.Dispose();
            }

            return(result);
        }