Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the images within shell32.dll in my C# project?

Tags:

c#

shell32.dll

How can I use the images within shell32.dll in my C# project?

like image 724
MaxCoder88 Avatar asked Jul 29 '11 12:07

MaxCoder88


5 Answers

This thread on the MSDN developer forums offers a solution:

The typical way to implement these in .NET is to use the graphics provided in the ZIP file located at C:\Program Files\Microsoft Visual Studio X\Common7\VS200XImageLibrary.

You don't state which version of Visual Studio you have installed but you'll need to replace the "200X" with your version number.

like image 160
ChrisF Avatar answered Oct 21 '22 20:10

ChrisF


You can extract icons from a DLL with this code:

public class IconExtractor
{

    public static Icon Extract(string file, int number, bool largeIcon)
    {
        IntPtr large;
        IntPtr small;
        ExtractIconEx(file, number, out large, out small, 1);
        try
        {
            return Icon.FromHandle(largeIcon ? large : small);
        }
        catch
        {
            return null;
        }

    }
    [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);

}

...

form.Icon = IconExtractor.Extract("shell32.dll", 42, true);

Of course you need to know the index of the image in the DLL...

like image 26
Thomas Levesque Avatar answered Oct 21 '22 20:10

Thomas Levesque


The code in the accepted answer leaks one icon handle each time it's called, as it always asks for two icon handles and only gives one back.

Here is a version that doesn't leak a handle:

public static Icon Extract(string filePath, int index, bool largeIcon = true)
{
    if (filePath == null)
        throw new ArgumentNullException(nameof(filePath));

    IntPtr hIcon;
    if (largeIcon)
    {
        ExtractIconEx(filePath, index, out hIcon, IntPtr.Zero, 1);
    }
    else
    {
        ExtractIconEx(filePath, index, IntPtr.Zero, out hIcon, 1);
    }

    return hIcon != IntPtr.Zero ? Icon.FromHandle(hIcon) : null;
}

[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, out IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);

[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, out IntPtr phiconSmall, int nIcons);
like image 36
Simon Mourier Avatar answered Oct 21 '22 18:10

Simon Mourier


See this code. It will be help

public class ExtractIcon
{
    [DllImport("Shell32.dll")]
    private static extern int SHGetFileInfo(
        string pszPath, uint dwFileAttributes,
        out SHFILEINFO psfi, uint cbfileInfo,
        SHGFI uFlags);

    private struct SHFILEINFO
    {
        public SHFILEINFO(bool b)
        {
            hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
        }
        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;
        public string szDisplayName;
        public string szTypeName;
    };

    private enum SHGFI
    {
        SmallIcon = 0x00000001,
        OpenIcon = 0x00000002,
        LargeIcon = 0x00000000,
        Icon = 0x00000100,
        DisplayName = 0x00000200,
        Typename = 0x00000400,
        SysIconIndex = 0x00004000,
        LinkOverlay = 0x00008000,
        UseFileAttributes = 0x00000010
    }

    public static Icon GetIcon(string strPath, bool bSmall, bool bOpen)
    {
        SHFILEINFO info = new SHFILEINFO(true);
        int cbFileInfo = Marshal.SizeOf(info);
        SHGFI flags;

        if (bSmall)
            flags = SHGFI.Icon | SHGFI.SmallIcon;
        else
            flags = SHGFI.Icon | SHGFI.LargeIcon;

        if (bOpen) flags = flags | SHGFI.OpenIcon;

        SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo, flags);

        return Icon.FromHandle(info.hIcon);
    }
}
like image 35
the1900 Avatar answered Oct 21 '22 19:10

the1900


Some of them are available in %Program Files%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary - for others, you'd need to speak to Microsoft's lawyers about licensing them for redistribution in your application

like image 40
Rowland Shaw Avatar answered Oct 21 '22 18:10

Rowland Shaw