Beschreibung:
Diese drei Klassen ermöglichen es bequem und unkompliziert das Icon einer Datei auszulesen. Dafür wird intern die SHGetFileInfo-Funktion verwendet. Der Code stammt größtenteils hieraus: MS Support Ich habe nur eine kleine Wrapper-Klasse geschrieben.
[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;
};
public 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);
}
public class FileIcon
{
public static Icon GetLargeIcon(string path)
{
return GetIcon(path, IconType.LargeIcon);
}
public static Icon GetSmallIcon(string path)
{
return GetIcon(path, IconType.SmallIcon);
}
public static Icon GetIcon(string path, IconType type)
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImgSmall = Win32.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)type);
Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
return icon;
}
public static Image GetImage(string path, IconType type)
{
return GetIcon(path, type).ToBitmap();
}
}
public enum IconType : uint
{
LargeIcon = 0x100,
SmallIcon = 0x100 | 0x1
};
Schlagwörter: SHGetFileInfo, Shell, FileInfo, Icon
As a man thinketh in his heart, so he is.