R – Windows chooses wrong icon from multi-icon file and self renders to correct size

iconsnetrenderingwindows

I have an .ico file with 5 icon sizes embedded in it being used as the main application icon and the System Tray icon.

When it shows up in the task bar the icon is using the 16×16 format which is desired.
When the icon shows up in the Notification Area/System tray, it is using the 32×32 format and Windows is rendering it down to a 16×16 icon, which looks horrible.

How do I force windows to use the 16×16 icon size in the notification area?
Here's my code to put the icon in the system tray:

ContextMenu cmNotify = new ContextMenu();
MenuItem miNotify = new MenuItem(Properties.Resources.Notify_Text);
miNotify.DefaultItem = true;
miNotify.Click += new EventHandler(notifyHandler);
cmNotify.MenuItems.Add(miNotify);


notifyIcon = new NotifyIcon();
notifyIcon.Icon = this.Icon;
notifyIcon.Visible = true;
notifyIcon.ContextMenu = cmNotify;
notifyIcon.Text = AppConstants.APPLICATION_NAME;

Best Answer

Both responses are close, but contain a subtle poison. You should not hardcode the requested size as 16x16.

Instead, query SystemInformation.SmallIconSize to determine the appropriate dimensions. Although the default is certainly 16x16, this could be changed by various things, such as DPI scaling.

See the MSDN article for more information on this property.

An example of usage would be

notifyIcon.Icon = new System.Drawing.Icon(this.Icon, SystemInformation.SmallIconSize),
Related Topic