Icons from a file 
Author Message
 Icons from a file

I have tried to read from earlier posts how to get the Icon from files when
trying to populate a treeview too look like the explorer.

This is the code I have created for getting the Icon, but it doesn't seam to
work. The icon appear to be empty so I guess there is something wrong with
how I use SHGetFileInfo.

I would be very thankful if anyone know what is wrong... Is there any
diffence when running the code under WindowsXP?

public const uint SHGFI_USEFILEATTRIBUTES = 0x4000;
public const uint FILE_ATTRIBUTRE_NORMAL = 0x4000;
public const uint SHGFI_SYSICONINDEX = 0x4000;
public const uint ILD_TRANSPARENT = 0x1;
public const uint SHGFI_ICON = 0x1; // large icon
public const uint SHGFI_LARGEICON = 0x0;// large icon
public const uint SHGFI_SHELLICONSIZE = 0x4;
public const uint SHGFI_SMALLICON = 0x1; // small icon
public const uint SHGFI_TYPENAME = 0x400;

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
out SHFILEINFO psfi, int cbFileInfo, uint uFlags);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
struct SHFILEINFO
{
    private const int MAX_PATH = 260;
    public IntPtr hIcon;
    public int iIcon;
    public int dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
    public string szTypeName;

Quote:
}

public static Icon getIcon(string fileName)
{
    SHFILEINFO info = new SHFILEINFO();
    IntPtr pointer = SHGetFileInfo(fileName, FILE_ATTRIBUTRE_NORMAL, out
info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI_SMALLICON |
SHGFI_USEFILEATTRIBUTES);

    //Retrieve the icon from the handle... (The Icon always get null in the
IconData...)
    System.Drawing.Icon ico = System.Drawing.Icon.FromHandle(pointer);
    return ico;

Quote:
}

//Mikael Petersson


Sun, 21 Mar 2004 21:41:50 GMT  
 Icons from a file
I'm working on the same problem. I have been able to get some information by
using the following the constants..

public const uint SHGFI_ICON =0x000000100; // get icon

public const uint SHGFI_DISPLAYNAME =0x000000200; // get display name

public const uint SHGFI_TYPENAME =0x000000400; // get type name

public const uint SHGFI_ATTRIBUTES =0x000000800; // get attributes

public const uint SHGFI_ICONLOCATION =0x000001000; // get icon location

public const uint SHGFI_EXETYPE =0x000002000; // return exe type

public const uint SHGFI_SYSICONINDEX =0x000004000; // get system icon index

public const uint SHGFI_LINKOVERLAY =0x000008000; // put a link overlay on
icon

public const uint SHGFI_SELECTED = 0x000010000; // show icon in selected
state

public const uint SHGFI_ATTR_SPECIFIED =0x000020000; // get only specified
attributes

public const uint SHGFI_LARGEICON =0x000000000; // get large icon

public const uint SHGFI_SMALLICON =0x000000001; // get small icon

public const uint SHGFI_OPENICON =0x000000002; // get open icon

public const uint SHGFI_SHELLICONSIZE =0x000000004; // get shell size icon

public const uint SHGFI_PIDL =0x000000008; // pszPath is a pidl

public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; // use passed
dwFileAttribute


Quote:
> I have tried to read from earlier posts how to get the Icon from files
when
> trying to populate a treeview too look like the explorer.

> This is the code I have created for getting the Icon, but it doesn't seam
to
> work. The icon appear to be empty so I guess there is something wrong with
> how I use SHGetFileInfo.

> I would be very thankful if anyone know what is wrong... Is there any
> diffence when running the code under WindowsXP?

> public const uint SHGFI_USEFILEATTRIBUTES = 0x4000;
> public const uint FILE_ATTRIBUTRE_NORMAL = 0x4000;
> public const uint SHGFI_SYSICONINDEX = 0x4000;
> public const uint ILD_TRANSPARENT = 0x1;
> public const uint SHGFI_ICON = 0x1; // large icon
> public const uint SHGFI_LARGEICON = 0x0;// large icon
> public const uint SHGFI_SHELLICONSIZE = 0x4;
> public const uint SHGFI_SMALLICON = 0x1; // small icon
> public const uint SHGFI_TYPENAME = 0x400;

> [DllImport("shell32.dll", CharSet=CharSet.Auto)]
> static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
> out SHFILEINFO psfi, int cbFileInfo, uint uFlags);
> [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
> struct SHFILEINFO
> {
>     private const int MAX_PATH = 260;
>     public IntPtr hIcon;
>     public int iIcon;
>     public int dwAttributes;
>     [MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
>     public string szDisplayName;
>     [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
>     public string szTypeName;
> }

> public static Icon getIcon(string fileName)
> {
>     SHFILEINFO info = new SHFILEINFO();
>     IntPtr pointer = SHGetFileInfo(fileName, FILE_ATTRIBUTRE_NORMAL, out
> info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI_SMALLICON |
> SHGFI_USEFILEATTRIBUTES);

>     //Retrieve the icon from the handle... (The Icon always get null in
the
> IconData...)
>     System.Drawing.Icon ico = System.Drawing.Icon.FromHandle(pointer);
>     return ico;
> }

> //Mikael Petersson



Mon, 22 Mar 2004 00:03:02 GMT  
 Icons from a file
the Problem is that u are passing the (IntPtr)pointer
rather u must pass the value info.hicon from the
SHfileinfo structure which SHgetfileinfo fills an then u
will get the data in the ico variable

IntPtr hTcdf = veShell.SHGetFileInfo(fileName,0,ref info,
System.Convert.ToUInt32(cbFileInfo), iFlags | uFlags);
IntPtr handleIco = info.hIcon;
ico = System.Drawing.Icon.FromHAndle(handleIco);

have fun

Quote:
>-----Original Message-----
>I have tried to read from earlier posts how to get the

Icon from files when
Quote:
>trying to populate a treeview too look like the explorer.

>This is the code I have created for getting the Icon, but
it doesn't seam to
>work. The icon appear to be empty so I guess there is

something wrong with
Quote:
>how I use SHGetFileInfo.

>I would be very thankful if anyone know what is wrong...
Is there any
>diffence when running the code under WindowsXP?

>public const uint SHGFI_USEFILEATTRIBUTES = 0x4000;
>public const uint FILE_ATTRIBUTRE_NORMAL = 0x4000;
>public const uint SHGFI_SYSICONINDEX = 0x4000;
>public const uint ILD_TRANSPARENT = 0x1;
>public const uint SHGFI_ICON = 0x1; // large icon
>public const uint SHGFI_LARGEICON = 0x0;// large icon
>public const uint SHGFI_SHELLICONSIZE = 0x4;
>public const uint SHGFI_SMALLICON = 0x1; // small icon
>public const uint SHGFI_TYPENAME = 0x400;

>[DllImport("shell32.dll", CharSet=CharSet.Auto)]
>static extern IntPtr SHGetFileInfo(string pszPath, uint
dwFileAttributes,
>out SHFILEINFO psfi, int cbFileInfo, uint uFlags);
>[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
>struct SHFILEINFO
>{
>    private const int MAX_PATH = 260;
>    public IntPtr hIcon;
>    public int iIcon;
>    public int dwAttributes;
>    [MarshalAs(UnmanagedType.ByValTStr,

SizeConst=MAX_PATH)]
Quote:
>    public string szDisplayName;
>    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
>    public string szTypeName;
>}

>public static Icon getIcon(string fileName)
>{
>    SHFILEINFO info = new SHFILEINFO();
>    IntPtr pointer = SHGetFileInfo(fileName,

FILE_ATTRIBUTRE_NORMAL, out
Quote:
>info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI_SMALLICON
|
>SHGFI_USEFILEATTRIBUTES);

>    //Retrieve the icon from the handle... (The Icon

always get null in the
Quote:
>IconData...)
>    System.Drawing.Icon ico =

System.Drawing.Icon.FromHandle(pointer);

- Show quoted text -

Quote:
>    return ico;
>}

>//Mikael Petersson

>.



Mon, 22 Mar 2004 20:41:21 GMT  
 Icons from a file
the Problem is that u are passing the (IntPtr)pointer
rather u must pass the value info.hicon from the
SHfileinfo structure which SHgetfileinfo fills an then u
will get the data in the ico variable

IntPtr hTcdf = veShell.SHGetFileInfo(fileName,0,ref info,
System.Convert.ToUInt32(cbFileInfo), iFlags | uFlags);
IntPtr handleIco = info.hIcon;
ico = System.Drawing.Icon.FromHAndle(handleIco);

have fun

Quote:
>-----Original Message-----
>I have tried to read from earlier posts how to get the

Icon from files when
Quote:
>trying to populate a treeview too look like the explorer.

>This is the code I have created for getting the Icon, but
it doesn't seam to
>work. The icon appear to be empty so I guess there is

something wrong with
Quote:
>how I use SHGetFileInfo.

>I would be very thankful if anyone know what is wrong...
Is there any
>diffence when running the code under WindowsXP?

>public const uint SHGFI_USEFILEATTRIBUTES = 0x4000;
>public const uint FILE_ATTRIBUTRE_NORMAL = 0x4000;
>public const uint SHGFI_SYSICONINDEX = 0x4000;
>public const uint ILD_TRANSPARENT = 0x1;
>public const uint SHGFI_ICON = 0x1; // large icon
>public const uint SHGFI_LARGEICON = 0x0;// large icon
>public const uint SHGFI_SHELLICONSIZE = 0x4;
>public const uint SHGFI_SMALLICON = 0x1; // small icon
>public const uint SHGFI_TYPENAME = 0x400;

>[DllImport("shell32.dll", CharSet=CharSet.Auto)]
>static extern IntPtr SHGetFileInfo(string pszPath, uint
dwFileAttributes,
>out SHFILEINFO psfi, int cbFileInfo, uint uFlags);
>[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
>struct SHFILEINFO
>{
>    private const int MAX_PATH = 260;
>    public IntPtr hIcon;
>    public int iIcon;
>    public int dwAttributes;
>    [MarshalAs(UnmanagedType.ByValTStr,

SizeConst=MAX_PATH)]
Quote:
>    public string szDisplayName;
>    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
>    public string szTypeName;
>}

>public static Icon getIcon(string fileName)
>{
>    SHFILEINFO info = new SHFILEINFO();
>    IntPtr pointer = SHGetFileInfo(fileName,

FILE_ATTRIBUTRE_NORMAL, out
Quote:
>info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI_SMALLICON
|
>SHGFI_USEFILEATTRIBUTES);

>    //Retrieve the icon from the handle... (The Icon

always get null in the
Quote:
>IconData...)
>    System.Drawing.Icon ico =

System.Drawing.Icon.FromHandle(pointer);

- Show quoted text -

Quote:
>    return ico;
>}

>//Mikael Petersson

>.



Mon, 22 Mar 2004 20:41:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to laod an icon from icon file?

2. how to get the icon of a file in .Net (equivalent to SHGetFileInfo )

3. How to get Icon of an file extension?

4. Icons associated with files

5. Extract icon from EXE file

6. Help with getting an Icon from a file

7. saving an icon to a file

8. Saving a resource icon to a file.

9. VC++ ( change icon of another file )

10. Icon Change in File Explorer

11. icon or Bitmap files

12. Urgent: How load icon from exe file

 

 
Powered by phpBB® Forum Software