Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get different sizes of the Windows system icons in .NET?

In particular I'd like to be able to get the small (16 x 16) icons at runtime.

I tried this:

new Icon(SystemIcons.Error, SystemInformation.SmallIconSize)

Which supposedly "attempts to find a version of the icon that matches the requested size", but it's still giving me a 32 x 32 icon. I also tried:

Size iconSize = SystemInformation.SmallIconSize;
Bitmap bitmap = new Bitmap(iconSize.Width, iconSize.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawIcon(SystemIcons.Error, new Rectangle(Point.Empty, iconSize));
}

But that just scales the 32 x 32 icon down into an ugly 16 x 16.

I've considered just pulling icons out of the VS Image Library, but I really want them to vary dynamically with the OS (XP icons on XP, Vista icons on Vista, etc.). I'm willing to P/Invoke if that's what it takes.

like image 470
Andrew Watt Avatar asked Jun 13 '10 04:06

Andrew Watt


People also ask

What size is a Windows desktop icon?

Icons have a maximum size of 256x256 pixels, making them suitable for high-dpi (dots per inch) displays.

What size should Windows 10 icons be?

Large Icons – 96 x 96 pixels (Automatically rendered by Windows from 256 version) Medium Icons – 48 x 48 pixels. Small Icons – 16 x 16 pixels.

How Big Should icons be on desktop?

Conclusions. A windows application should include the standard icon sizes 16, 32, 48 and 256. If you want to cover all the standard resolutions, include the additional icon sizes 20, 24, 40, 60, and 72. If you want to cover every possible resolution, include icons sizes 16, 17, 18, ...., 255, 256.


1 Answers

You have to scale them yourself. The SystemIcons, as you found out, only have 32x32. You can easily scale them to 16 or 48 as needed. Use interpolation to get a nice bicubic resize. We've done this many times successfully to create very nice looking 16x16 versions and it works fine whether running XP or Vista or 7.

Size iconSize = SystemInformation.SmallIconSize;
Bitmap bitmap = new Bitmap(iconSize.Width, iconSize.Height);

using (Graphics g = Graphics.FromImage(bitmap))   
{
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.DrawImage(SystemIcons.Error.ToBitmap(), new Rectangle(Point.Empty, iconSize));   
}

Icon smallerErrorIcon = Icon.FromHandle(bitmap.GetHicon());

Be sure to check out the MSDN example here, especially their use of the DestroyIcon method to clean up the handle when you're done.

like image 170
Sean Hanley Avatar answered Nov 07 '22 15:11

Sean Hanley