Skip to content

Instantly share code, notes, and snippets.

@AlexanderRasch
Created August 10, 2013 19:30
Show Gist options
  • Save AlexanderRasch/6201814 to your computer and use it in GitHub Desktop.
Save AlexanderRasch/6201814 to your computer and use it in GitHub Desktop.
//The might .dll shell32 behold !
[DllImport("Shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
// Constants that we need in the function call
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
// This structure will contain information about the file
public struct SHFILEINFO
{
// Handle to the icon representing the file
public IntPtr hIcon;
// Index of the icon within the image list
public int iIcon;
// Various attributes of the file
public uint dwAttributes;
// Path to the file
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szDisplayName;
// File type
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
//Sample Usage, not (to) advanced ? =)
private void DrawIcon(string strFilePath)
{
//Store the 'Icon'
Icon myIcon;
//Large icon
IntPtr hImgLarge;
//;) fancy shit
SHFILEINFO shinfo = new SHFILEINFO();
///////////////////////////////
//DRAW THE LARGE ICON
///////////////////////////////
//Get handle
hImgLarge = SHGetFileInfo(txtFilePath.Text, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
//myIcon equal icon from file
myIcon = Icon.FromHandle(shinfo.hIcon);
//Draw icon inside picture box
picLarge.Image = myIcon.ToBitmap();
//Clean up and jump out
GC.Collect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment