Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include an icon once that can be used for both the executable and a form?

I'm trying to use a single .ico file (with multiple sizes) for both the Application executable and a form inside the application, without including the icon in the executable twice.

I noticed this because my app (without icons) is 600KB compiled, and the icon is 300KB, but when I use both the app increases to 1200KB compiled, indicating it's being embedded twice.

Here's what I've tried:

(1) Pick icon files using UI

  1. Go to Application Properties > Application > Resources > Icon and use the "..." button to pick MyIcon.ico file.
    • Compiled exe is now 900KB
  2. Go to Form Properties > Icon and use "..." button to choose MyIcon.ico file.
    • Compiled exe is now 1200KB

(2) Use resource

  1. Go to Application Properties > Resources > Icons > Add existing file and pick MyIcon.ico file
  2. In form constructor, add: this.Icon = Properties.Resources.MyIcon;
    • Compiled exe is now 900KB
  3. Go to Application Properties > Application > Resources > Icon, and choose Resources\MyIcon.ico (which is listed in the drop-down)
    • Compiled exe is now 1200KB

Clearly, it's still including the file a second time, not referencing an embedded resource.

(3) Use Icon.ExtractAssociatedIcon()

  1. Go to Application Properties > Application > Resources > Icon and use the "..." button to pick MyIcon.ico file.
    • Compiled exe is now 900KB
  2. In form constructor, add this.Icon = Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.FriendlyName);
    • Compiled exe is still 900KB, but icon is the generic "exe" icon from Windows, not my application's icon

Before I go deeper into this, am I missing something obvious? Is there a standard way to do this? Am I just not using Icon.ExtractAssociatedIcon() properly?

like image 685
gregmac Avatar asked Oct 29 '22 06:10

gregmac


1 Answers

I'm sorry, this is my oversight. Method (3) does actually work.

I was running this from the VisualStudio debugger, but didn't notice the .vshost.exe file gets a different icon -- which is what was showing up in the form.

When the compiled exe is used directly, it works fine.

like image 137
gregmac Avatar answered Nov 15 '22 13:11

gregmac