none
Capture Icons

    Question

  • Hello all
    i want to know how to capture the icon of a file or folder , or at least create shortcut icon for it , and then use it in my application
    thx all:)
    Tuesday, January 17, 2006 12:22 PM

Answers

  • Ok,so this is the link you need:

    http://www.codeproject.com/cs/miscctrl/FilesListBox.asp

    This is the class you need:

     

    /// <summary>

    /// Util class to extract icons from files or directories.

    /// </summary>

    class IconExtractor

    {

    [StructLayout(LayoutKind.Sequential)]

    public struct SHFILEINFO

    {

    public IntPtr hIcon;

    public IntPtr iIcon;

    public uint dwAttributes;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

    public string szDisplayName;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

    public string szTypeName;

    };

    class Win32

    {

    public const uint SHGFI_ICON = 0x100;

    public const uint SHGFI_LARGEICON = 0x0; // 'Large icon

    public const uint SHGFI_SMALLICON = 0x1; // 'Small icon

    [DllImport("shell32.dll")]

    public static extern IntPtr SHGetFileInfo(string pszPath,

    uint dwFileAttributes,

    ref SHFILEINFO psfi,

    uint cbSizeFileInfo,

    uint uFlags);

    }

    /// <summary>

    /// Gets the icon asotiated with the filename.

    /// </summary>

    /// <param name="fileName"></param>

    /// <returns></returns>

    public static Icon GetFileIcon(string fileName, IconSize _iconSize)

    {

    System.Drawing.Icon myIcon = null;

    try

    {

    IntPtr hImgSmall; //the handle to the system image list

    SHFILEINFO shinfo = new SHFILEINFO();

    //Use this to get the small Icon

    hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo,

    (uint)Marshal.SizeOf(shinfo),

    Win32.SHGFI_ICON |

    (_iconSize == IconSize.Small ? Win32.SHGFI_SMALLICON : Win32.SHGFI_LARGEICON) );

    //The icon is returned in the hIcon member of the shinfo

    //struct

    myIcon =

    System.Drawing.Icon.FromHandle(shinfo.hIcon);

    }

    catch

    {

    return null;

    }

    return myIcon;

    }

    }

    /// <summary>

    /// Specifies the icon size (16 or 32)

    /// </summary>

    public enum IconSize

    {

    /// <summary>

    /// 16X16 icon

    /// </summary>

    Small,

    /// <summary>

    /// 32X32 icon

    /// </summary>

    Large

    }

     

    Call :

    Icon fileIcon = IconExtractor.GetFileIcon(fullFileName, _IconSize.Small);

     

     

    Tuesday, January 17, 2006 4:44 PM

All replies

  • Tuesday, January 17, 2006 2:59 PM
  • thx eli for your quick response and your interest

    but for sorry! the topic that u sent to me not covers my question

    any way the article that u sent to was a very good one

    and i will try to find an answer to my question and if i found it i will tell u

    Tuesday, January 17, 2006 3:53 PM
  • Try to explain again what you are tring to do?
    Tuesday, January 17, 2006 3:55 PM
  • I think I understand now, you want to find what is the icon of a file, like for msword.exe, you want to get the Word Icon?

    if so, check out my articale:

    http://www.codeproject.com/cs/miscctrl/FilesListBox.asp

     

    and look at the IconExtractor  util class.

     

    Tuesday, January 17, 2006 3:57 PM
  • You could also have a look at the new method for .NET 2.0 called Icon.ExtractAssociatedIcon, however this won't retrieve icons for folders.
    Tuesday, January 17, 2006 4:27 PM
    Moderator
  • Yes David you are right, but yet, it have very limited access comparing to the native windows api, not only for folders but also for some file types.
    Tuesday, January 17, 2006 4:29 PM
  • let me tell u what i want to do , i want to drag files or folders from windows explorer and drop them in my windows forms listview control then create shortcuts for them in my list view so user can double click in the item in my listview to open these files or folders , i had did all the work except the icons of the files or folders
    Tuesday, January 17, 2006 4:40 PM
  • Ok,so this is the link you need:

    http://www.codeproject.com/cs/miscctrl/FilesListBox.asp

    This is the class you need:

     

    /// <summary>

    /// Util class to extract icons from files or directories.

    /// </summary>

    class IconExtractor

    {

    [StructLayout(LayoutKind.Sequential)]

    public struct SHFILEINFO

    {

    public IntPtr hIcon;

    public IntPtr iIcon;

    public uint dwAttributes;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

    public string szDisplayName;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

    public string szTypeName;

    };

    class Win32

    {

    public const uint SHGFI_ICON = 0x100;

    public const uint SHGFI_LARGEICON = 0x0; // 'Large icon

    public const uint SHGFI_SMALLICON = 0x1; // 'Small icon

    [DllImport("shell32.dll")]

    public static extern IntPtr SHGetFileInfo(string pszPath,

    uint dwFileAttributes,

    ref SHFILEINFO psfi,

    uint cbSizeFileInfo,

    uint uFlags);

    }

    /// <summary>

    /// Gets the icon asotiated with the filename.

    /// </summary>

    /// <param name="fileName"></param>

    /// <returns></returns>

    public static Icon GetFileIcon(string fileName, IconSize _iconSize)

    {

    System.Drawing.Icon myIcon = null;

    try

    {

    IntPtr hImgSmall; //the handle to the system image list

    SHFILEINFO shinfo = new SHFILEINFO();

    //Use this to get the small Icon

    hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo,

    (uint)Marshal.SizeOf(shinfo),

    Win32.SHGFI_ICON |

    (_iconSize == IconSize.Small ? Win32.SHGFI_SMALLICON : Win32.SHGFI_LARGEICON) );

    //The icon is returned in the hIcon member of the shinfo

    //struct

    myIcon =

    System.Drawing.Icon.FromHandle(shinfo.hIcon);

    }

    catch

    {

    return null;

    }

    return myIcon;

    }

    }

    /// <summary>

    /// Specifies the icon size (16 or 32)

    /// </summary>

    public enum IconSize

    {

    /// <summary>

    /// 16X16 icon

    /// </summary>

    Small,

    /// <summary>

    /// 32X32 icon

    /// </summary>

    Large

    }

     

    Call :

    Icon fileIcon = IconExtractor.GetFileIcon(fullFileName, _IconSize.Small);

     

     

    Tuesday, January 17, 2006 4:44 PM
  • Many thx, it works and i did most of the work using it 

    and this article was very helpful

    Monday, January 23, 2006 9:29 AM