Explore the Visual Studio Image Library in C#

[image library]

This example explains where you can find standard images in the Visual Studio Image Library that you can use in your applications.

The example Give transparent backgrounds to images displayed on buttons in C# explains how to make some pixels in a bitmap transparent so an image looks nice on a button. Bitmap, jpg, and many other graphic files cannot store transparent pixels but PNG images can. That means you can create an image with transparent pixels, save it in a PNG file, and then load the PNG file into a button to get a nice result.

Sometimes you may want standard images for common commands such as next, previous, undo, filter, magnify, and so forth. The Visual Studio Image Library contains more than 1,000 images including hundreds of button images in PNG format. This example shows samples of buttons using those PNG images.

The library used to be installed when you installed Visual Studio, but I don’t think it is any more. (I’m not completely sure because I have so many versions of Visual Studio installed.) If the image library is installed, it will probably be in a zip file with a name similar to VS2010ImageLibrary.zip. This file is typically installed in a folder such as ..\Program Files\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033\.

If the library isn’t installed, you can download it here. That page lets you select different versions of the library. Currently you can select the VS 2012 version (19.4MB), the VS 2013 version (285.6 MB), or the VS 2015 version (2.9 GB!).

The image library’s current description on the download page says:

The Visual Studio Image Library contains application images that appear in Microsoft Visual Studio, Microsoft Windows, the Office system and other Microsoft software.

This example uses the Visual Studio 2012 library because I didn’t want to wait 2 hours to download the VS 2015 version. The program displays PNG files in the \VS2012 Modern Image Library\Objects\png subdirectory.

To make managing the PNG files easier within the project, I right-clicked the project in Solution Explorer, opened the Add menu, and selected New Folder. I named the folder Buttons and then used Windows Explorer to copy the 32×32 pixel PNG files into the new directory.

Tip: That directory contains 752 PNG files, only 64 of which are 32×32 pixels. To find the images with a certain size, open Windows Explorer in Details view, right-click on the column headings and check Dimensions. Then click on the Dimensions column to order the images by their dimensions.

Next I right-clicked the Buttons folder in Solution Explorer, opened the Add command, and selected Existing Item. I selected all of the files (changed the dialog’s filter to All Files, because it initially only lists C# code files) and clicked Add.

Finally I selected all of the PNG files in Solution Explorer and set their “Copy to Output Directory” properties to “Copy if newer.” This may all seem like a hassle but it lets the program assume that the files will be in the startup directory’s Buttons subdirectory.

When it starts, the program uses the following code to display the images in the Buttons folder.

// Display all of the images in the Buttons folder.
private void Form1_Load(object sender, EventArgs e)
{
    const int wid = 32;
    const int hgt = 32;
    const int margin = 3;
    int x = margin;
    int y = margin;
    const int num_columns = 10;
    const int xmax = num_columns * (wid + margin);

    // Find the images.
    foreach (string filename in
        Directory.GetFiles("Buttons", "*.png"))
    {
        // Make a new Button.
        Button btn = new Button();
        btn.Parent = this;
        btn.Image = new Bitmap(filename);
        btn.Size = new Size(32, 32);
        btn.Location = new Point(x, y);
        x += wid + margin;
        if (x > xmax)
        {
            x = margin;
            y += hgt + margin;
        }

        FileInfo file_info = new FileInfo(filename);
        tipButton.SetToolTip(btn, file_info.Name);
    }

    // Size the form to fit.
    this.ClientSize = new Size(
        xmax + margin,
        y + hgt + margin);
}

The code uses the Directory.GetFiles method to find PNG files in the Buttons directory. It loops over the files and creates a Button control for each.

For each file, the program adds the Button to the form, positions it, and sets its Image to display its PNG file. It then sets the Button control’s tooltip to display the file’s name. It uses a FileInfo object to display only the file’s name without the path.

Now download one of the image libraries and explore. You’ll find thousands of PNG, BMP, and ICO files taht you can include in your applications. That is if you can find the images that you want.

You may also want to search the internet for images. Often you can find more colorful and engaging images that you can use.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in controls, graphics and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *