Reusing The .NET Application Icon
Asked Answered
P

2

5

How can I reuse the application icon from within my application so I don't have to embedded it twice (once for the application icon and once for internal usage)?

Pontic answered 29/8, 2009 at 18:11 Comment(0)
T
4

You can read it back via P/Interop calls. It goes something like this:

static Icon GetAppIcon() {
    var fileName = Assembly.GetEntryAssembly().Location
    System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
    if (!hLibrary.Equals(System.IntPtr.Zero)) {
        System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
        if (!hIcon.Equals(System.IntPtr.Zero)) {
            return Icon.FromHandle(hIcon);
        }
    }
    return null; //no icon was retrieved
}

additionally, native signatures are:

private static class NativeMethods {
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    static extern internal IntPtr LoadIcon(IntPtr hInstance, string lpIconName);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
     static extern internal IntPtr LoadLibrary(string lpFileName);
}
Trochal answered 29/8, 2009 at 18:17 Comment(0)
A
6

It seems easiest to use Icon.ExtractAssociatedIcon, as outlined in this related question: Avoiding duplicate icon resources in a .NET (C#) project

Aeolic answered 29/8, 2009 at 18:25 Comment(0)
T
4

You can read it back via P/Interop calls. It goes something like this:

static Icon GetAppIcon() {
    var fileName = Assembly.GetEntryAssembly().Location
    System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
    if (!hLibrary.Equals(System.IntPtr.Zero)) {
        System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
        if (!hIcon.Equals(System.IntPtr.Zero)) {
            return Icon.FromHandle(hIcon);
        }
    }
    return null; //no icon was retrieved
}

additionally, native signatures are:

private static class NativeMethods {
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    static extern internal IntPtr LoadIcon(IntPtr hInstance, string lpIconName);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
     static extern internal IntPtr LoadLibrary(string lpFileName);
}
Trochal answered 29/8, 2009 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.