Solved! See EDIT 2 for working code.
Based on the code of this site and the comments made by freundblase on the same site, I have the following class to get jumbo default icons based on file extension:
I am trying to use the class as follows:
preview.ImageSource = IconManager.GetIcon(".pdf", true);
where the preview is an ImageBrush object.
But I get the following image:

Yes, I tried with registered file types such as pdf files.
What's wrong?
TIA
-
EDIT 1:
I think the icon is used for unregistered file types.
If I use the actual file, I get a FileNotFoundException.
-
EDIT 2:
Before posting this question, I did a code cleanup using ReSharper.
I created the class again based on the site code and freundblase comments, and now it works!
I need to carefully analyze the changes made by ReSharper. Clearing the code never caused me any problems.
For posterity, the following code works:
public class IconManager { // Constants that we need in the function call private const int SHGFI_ICON = 0x100; private const int SHGFI_LARGEICON = 0x0; private const int SHGFI_SMALLICON = 0x1; private const int SHIL_EXTRALARGE = 0x2; private const int SHIL_JUMBO = 0x4; private const int WM_CLOSE = 0x0010; // 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 = 260)] public string szDisplayName; // File type [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }; [DllImport("Kernel32.dll")] public static extern Boolean CloseHandle(IntPtr handle); private struct IMAGELISTDRAWPARAMS { public int cbSize; public IntPtr himl; public int i; public IntPtr hdcDst; public int x; public int y; public int cx; public int cy; public int xBitmap; // x offest from the upperleft of bitmap public int yBitmap; // y offset from the upperleft of bitmap public int rgbBk; public int rgbFg; public int fStyle; public int dwRop; public int fState; public int Frame; public int crEffect; } [DllImport("user32")] private static extern IntPtr SendMessage( IntPtr handle, int Msg, IntPtr wParam, IntPtr lParam ); [StructLayout(LayoutKind.Sequential)] private struct IMAGEINFO { private readonly IntPtr hbmImage; private readonly IntPtr hbmMask; private readonly int Unused1; private readonly int Unused2; private readonly RECT rcImage; }
user1537004
source share