Give an application beautiful icons in C#
Contents
Icon Sizes
When you make an application, its icons appear in at least 10 ways:- In the form's title bar
- In the Windows Alt-Tab task switcher
- In the Windows Taskbar
- In Windows Explorer in these views:
- Extra Large Icons
- Large Icons
- Medium Icons
- Small Icons
- List
- Details
- Tile
| For best results, the icon file must at least include images at the sizes 16 x 16, 32 x 32, 48 x 48, and 256 x 256 pixels. |
Creating an Icon File
There are many ways to make an icon file. One obvious option is to use the icon editor integrated with Visual Studio. Unfortunately this editor isn't included with all version of Visual Studio so if you have the Express Edition, it may not be there. You can also use third party icon editing tools. Both of these approaches are described below.Using the Integrated Icon Editor
Using External Icon Editors
Instead of using the integrated icon editor, you can use an external tool. There are lots of icons editors out there. I'm going to mention only one approach. That doesn't mean it's necessarily better than the others, but it is free and you can use it to produce a good result. I;ll walk you through creating the icon shown at the beginning of this article. The tools I used to create this example's icon file are Inkscape and @icon sushi. Inkscape is a drawing tool and @icon sushi is an icon converter that can turn images into icon files. To create the icon, follow these steps:Inkscape
- Download and install Inkscape
- Start the program. Initially it will create a fairly large document. Open the File menu, select Document Properties, and set the document's size to 256 x 256 pixels.
- To make working with the document easier, turn on the design grid. In the Document Properties window, click the Grid tab. Make sure Rectangular Grid is selected and click New.
- Now draw the icon. For this example:
- Select the circle tool and draw a big circle. If you hold down the Ctrl key while drawing, Inkscape will keep it round.
- Select the object selection tool, right-click the circle, and select Fill and Stroke.
- On the Fill and Stroke window's Fill tab, click the Linear Gradient button. Then click the Edit button.
- Select the gradient's first stop from the dropdown and set its color to a dark green.
- Select the gradient's second stop from the dropdown and set its color to a light green. Be sure to set its Alpha component to 255 or else the color will be transparent.
- Close the gradient editor.
- Back on the main design surface, select the Create and Edit Gradients tool, which looks like a line segment connecting two little boxes. Click and drag the boxes so the circle is dark in the upper left and light in the lower right.
- On the main design surface, select the object selection tool, click on the circle, and press Ctrl-D to make a duplicate.
- Click and drag one of the duplicate's corners to make it smaller. If you hold down the Ctrl key, Inkscape will keep it round.
- Press Ctrl-A to select all objects. Then open the Object menu and select Align and Distribute. On the window that appears, click the Center on Vertical Axis and Center on Horizontal Axis buttons to center the two circles.
- Back on the design surface, click off of the circles to deselect them. Then select the smaller circle and use the Create and Edit Gradients tool to reverse the direction of its gradient so it is light in the upper left and dark in the lower right.
- Save the file as Circle.svg or whatever you want to call it.
- Next export the file.
- Open the File menu and select Export Bitmap.
- On the window that appears, set the size to 256 x 256 pixels.
- Enter a file name such as Circle256.png and click Export.
- To resize the image for the next file, open the File menu, select Document Properties, go to the Page tab, and set the page size to 48 x 48 pixels.
- On design surface, click Ctrl-A to select both circles. Press Ctrl-G to group them. Then use the grab handles to resize the circles so they fit on the new document size.
- Export the new image into file Circle48.png. Important Note: Be sure to check the bitmap's size in the Export Bitmap window. It tends to reset the size to what it thinks best and that may not be 48 x 48. If the file is saved at the wrong size by even 1 pixel, the results will be bad.
- Repeat these steps until you've saved the image at 256 x 256, 48 x 48, 32 x 32, and 16 x 16 sizes.
- Note: At the smaller sizes, the icon isn't big enough for users to really see the color gradient so the two circles lose their contrast and seem to blur together. You can try to adjust the gradients to make them more obvious or you can just give up on the gradients and make the two circles different solid colors. For example, you might make the bottom (outer) circle dark green and the upper (inner) circle light green.
- Note: If you want to be able to reproduce the image at all sizes, save the file each time you resize it with a different file name.
- After you have saved the image at all of the necessary sizes, close Inkscape.
@icon sushi
- Start @icon sushi.
- Open the File menu and select Open. Select all of your files and click Open.
- Press Ctrl-A to select all of the files.
- Press Ctrl-M to save the files as a multiple file icon file. Enter the file name and click Save.
- That's all there is to it. Close @icon sushi.
Using an Icon File
A C# application displays icons in two ways. First, Windows Explorer displays the application's icon. Second, each form has its own icon that it displays in its title bar, in the Taskbar, and in the Alt-Tab task switcher. To set the application's icon, open the Project menu and select Properties. On the Application tab, click the ellipsis to the right of the Icon dropdown, select the icon file, and click Open. To set a form's icon, open the form in the form designer and click on the form. In the Properties window, select the Icon property and click the ellipsis to the right. Select the icon file and click Open.Clearing the Windows Explorer Icon Cache
One hurdle to testing and tweaking icons is the fact that Windows Explorer caches icons to save time. That means if you give an application an icon and then look at the application in Windows Explorer, the icon is cached. If you later change the icon, Windows Explorer won't see the new icon because it is still using the one it has cached. To clear the cache, close all instances of Windows Explorer. Now change the system's icon size and then change it back. How you do this will depend on the version of Windows that you have. Search the Internet for instructions for your particular version of Windows. In Windows Vista, right-click the desktop and select Personalize. Click Window Color and Appearance, and then click the Advanced button. Select the Icon item and change the size. Close the dialog and click Apply on the previous dialog. Then go back and reset the size to the original value. If this doesn't work or you can't figure out how to resize the icons on your system, try rebooting.If you make a beautiful icon that you'd like to share, let me know or post a comment including a link to it.



Comments