C # - WPF - Project size after adding icons (loading icon from resources or pulling from an EXE project)

Edit1: edited title

Edit2: method 3 added

Edit3: method 4 added


I am creating a very small application / utility, only 20 KB. My icon file, which contains various sizes, is 40 KB.

As I add my icon by right-clicking my project in the solution explorer and clicking "Existing item ...", I again right-clicked my project, go to "Properties" → "Application", and in the "Icon and manifest "I choose my new icon for the entire project.

Doing this shows my icon in all my forms, exe, taskbar, etc. With the exception of the System Tray icon.

How I load the system tray icon by extracting the icon from the EXE:

Method 1:

Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
notifyIcon.Icon = ico;

now my total project size with all the icons loaded is 20 KB + 40 KB = 60 KB - this is what I get after.

I wanted to know if all my icons load correctly?

I know that I can add my icon to the "Resources" project and do:

Method 2:

notifyIcon.Icon = PROJECTNAME.Properties.Resources.icon;

But this only doubles the size of the icon, and then adds the size of the project on top. So:

20 KB (project size) + 40 KB (EXE icon) + 40 KB (resource icon) = 100 KB

Method 3

Discord has posted in the answers below another way to capture a file directly from executable resources.

IntPtr hIcon = LoadIcon(GetModuleHandle(null), new IntPtr(32512));
notifyIcon.Icon = Icon.FromHandle(hIcon);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon (IntPtr hInstance, IntPtr iconName);
[DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle (string moduleName);

The only problem with method 1 and 3 right now is that the icon is extracted at 32x32 and then reduced to 16x16, which does not look good in the system tray.

"vanmelle": .NET(#) , 16x16, , 32x32 HiDPI

4

EXE, , 16x16 32x32 . , , HiDPI. , , 16x16 32x32 , : ( , 16x16, 32x32 - 4 )

notifyIcon.Icon = new System.Drawing.Icon(Properties.Resources.ico, System.Windows.Forms.SystemInformation.SmallIconSize);

16x16 32x32, .

:

notifyIcon.Icon = new System.Drawing.ico;

32x32 16x16, .


, , "" notifyIcon. - → "" → "" . , ( 20 + 40 ) 100

1. , , ?

, 60 100 , . , , - ?

- # WPF.

+4
2

, , ( .rsrc) 32512 (IDI_APPLICATION). Icon.ExtractAssociatedIcon, - LoadIcon. .NET, , :

IntPtr hIcon = LoadIcon(GetModuleHandle(null), new IntPtr(32512));
notifyIcon.Icon = Icon.FromHandle(hIcon);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon (IntPtr hInstance, IntPtr iconName);
[DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle (string moduleName);

. .exe Windows Forms, .

WPF HICON ImageSource, , , , (. Imaging.CreateBitmapSourceFromHIcon ..).

, Icon.ExtractAssociatedIcon . , , .

. .NET #.

+2

. - , , , , .

WPF , WPF.exe, .

0

Source: https://habr.com/ru/post/1589511/


All Articles