问题
I have an application whose size is 16kb.
After adding an icon resource through the Project Properties menu, the application, as expected, increased to a size of 299kb.
Now, under Properties/Application when I set the Icon File to "Resource\IconName.ico" the file size again increases to 581kb.
Is this normal behavior? I understand that it increases when I add the icon as a resource, but not when I set the icon from the resources to the Application Icon.
Can anyone explain why this is happening?
EDIT:
Maybe a better question would be how can I set the Application Icon using an icon from the Properties/Resources section?
回答1:
Yes, this is normal behaviour. Your icon isn't being stored as a resource twice (as suggested in some comments), it's simply being linked into the exe during compilation while also being stored as a resource. Since the icon is public-facing (i.e. explorer.exe accesses it directly when rendering the icon), you essentially end up with 2 copies of the icon in different formats:
- One copy of the icon compiled directly into the exe in a Windows-standard format
- One copy as a .NET resource
If you really want to save this extra space, remove the embedded resource, change your application icon reference to point straight to the icon on disk, then, when you need a copy of the icon to assign to your forms, extract it directly from the assembly:
var executablePath = Assembly.GetExecutingAssembly().Location;
var icon = Icon.ExtractAssociatedIcon(executablePath);
来源:https://stackoverflow.com/questions/12030981/application-size-increases-when-setting-application-icon-from-resources