Skip to content

Getting Folders Icon Image...Unable to take large Icons...

Suraj Rai

Asked Jan 31, 2007, 9:12 PM EST1 answer
Share
LinkedInX

Hi,

I am using SHGetFileInfo Function to Get the Icons That Are Associated with Files and
folders.

I got the code from an article . http://www.codeguru.com/Csharp/Csharp/cs_misc/icons/article.php/c4261/

The code goes like this :

At the beggining of Namespace :

[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);
}


in button click :

             IntPtr hImgSmall;
            IntPtr hImgLarge;
            YuntaaControls.SHFILEINFO shinfo = new YuntaaControls.SHFILEINFO();
            YuntaaControls.SHFILEINFO Lginfo = new YuntaaControls.SHFILEINFO();


            hImgSmall = YuntaaControls.Win32.SHGetFileInfo(LocalPath, 0, ref shinfo,
                                       (uint)Marshal.SizeOf(shinfo),
                                        YuntaaControls.Win32.SHGFI_ICON |
                                        YuntaaControls.Win32.SHGFI_LARGEICON);

            // For large Icon
            //hImgLarge = YuntaaControls.Win32.SHGetFileInfo(LocalPath, 0, ref shinfo,
            //                           (uint)Marshal.SizeOf(shinfo),
            //                            YuntaaControls.Win32.SHGFI_ICON |
            //                            YuntaaControls.Win32.SHGFI_LARGEICON);


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

            //System.Drawing.Icon myLgIcon =
            //      System.Drawing.Icon.FromHandle(shinfo.hIcon);

           
          imgListYuntaaFileBrowse.Images.Add(myIcon);


Every thing goin fine. The only problem is that i am not been able to take
the bigger Icon. the Icons are coming very small.

What I want is that when we click "Save As" we get one Dialog box from where
we can browse and save our file.

If we see the left side of that dialogbox
we see some big icons for "Desktop","my recent documents", "My Computer" n so on.

how to get thouse big icons. Is there any way that we can the desired size.
Something like we can mention the icon size and we will get that.

If anyone has any Idea. Do help.

Thanks

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I litteraly spent 4 hours on this. The issue is not in the code, which can be found here. http://support.microsoft.com/kb/319340/ The issue is the image list. By default it is shrinking the icon to 16x16 rather then displaying it at 32x32. Just set your imagelist to use 32x32 and the image will display right. Talk about a simple issue that just pisses you off.
    0