Click here to Skip to main content
15,612,449 members
Articles / Web Development / ASP.NET
Posted 29 May 2005


34 bookmarked

Associated Icons Image Control

Rate me:
Please Sign up or sign in to vote.
3.92/5 (9 votes)
1 Jun 20055 min read
A web control that displays the icon image currently associated with a given file or file extension.


This custom server control displays the icon that is currently associated with the given file extension. The file icon associations are kept by the operating system and can be retrieved with a single Win32 API call.

Use this control when you just need to display a well known file icon or even when you want to show any list of files with their appropriate icons.

Using the code

The article download contains a VS.NET 2003 project of a sample ASP.NET application. This application contains the two files that hold the important part of the code. These files are AssociatedIcon.cs and IconExtractor.cs.

Instead of just extracting the whole project from the zipped file, I'd like to suggest that you use your own installation of Visual Studio .NET to create a new C# ASP.NET project named AssociatedIcons. Add two folders to your application, Controls and IconStubs. Delete the default Webform1.aspx file and add a new WebForm named TestPage.

To save some major typing time, just get the file IconExtractor.cs and put it in the Controls folder of your project. Add a new class named AssociatedIcon in the same Controls folder. This class will be our custom control.

After all that, your project should be looking like the image below:

Image 1

Add the following code to the AssociatedIcon.cs file that you just created:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace AssociatedIcons.Controls
    public class AssociatedIcon : Image
         * This control accepts a file name or file extension,
         * determines the associated icon, and displays it on the page
         * ***/

        /// <summary>
        /// Gets or sets a file name or extension (including the dot)
        /// to display the associated icon
        /// </summary>
        public string ExtensionOrFile
                string s = (string)ViewState["ExtensionOrFile"];
                if(s == null) return "";
                return s;
            set{ ViewState["ExtensionOrFile"] = value; } 

        /// <summary>
        /// Determines if the icon size will be 16x16 or 32x32 pixels
        /// </summary>
        public bool LargeIcon
                object obj = ViewState["LargeIcon"];
                if(obj == null) return false;
                return (bool)obj;
                ViewState["LargeIcon"] = value;

        public override string ImageUrl
            get{ return base.ImageUrl;}
            set{ throw new 
                 InvalidOperationException("The ImageUrl cannot be set.");}

        protected override void OnLoad(EventArgs e)
                base.ImageUrl = 
                  IconExtractor.GetUrlToIcon(ExtensionOrFile, LargeIcon);


Because we will be writing files in the IconStubs folder, you'll need to give write permission to the ASPNET user in that folder. In case you have never done this before, the process is simple. In any Windows Explorer window, find the IconStubs folder, right-click it and select Properties. Go to the Security tab. If the Allow inherited permissions[...] checkbox is checked, uncheck it. Use the Add button to add the ASPNET user to the list (in case it's not already there). With the ASPNET user in the list, select it and choose only the Allow Write checkbox in the permissions list. See the image below. After that, click OK and you're done.

Image 2

How this control works

As you can see from the code above, our control is a fairly a simple extension of the System.Web.UI.WebControls.Image server control. We added a property to hold the desired file extension (or file name) and another one to specify if the desired icon will be the small (16 x 16 pixels) or the large (32 x 32 pixels) version. We also inhibit the setting of the ImageUrl property directly so that nobody changes it inadvertently.

As you can also notice, there's not much going on in this control's code. The real meat of the process is implemented by the IconExtractor class.

The IconExtractor class

The central piece of the icon extraction process is a call to the SHGetFileInfo Shell32 Windows API. This API, among other things, give us a handle to the associated icon (small or large) for the given file. After that, the code will save the icon as a PNG image with a transparent background in the IconStubs folder. Next time the same extension icon is requested, the URL to the already existing image file will be returned, without any new file creation. After a while, you should get your IconStubs folder full of the images for all the icons you used, as shown below:

Image 3

Note: IE (at least the version I have: 6.0.2800.1106) has a flaw in transparent PNG rendering, causing the transparent areas to render gray. The code includes comments that help you change the implementation from PNG to GIF, losing the transparent background.

Note: The implementation of transparent background in GIF images is way too complicated in .NET, involving unsafe blocks of code, so I decided not to include it in this sample, for the sake of simplicity (or laziness, if you'd like me to be really honest :) ).

Test driving the control

To see how the control works, let's put some content in our TestPage.aspx. First, let's register a tag prefix with our Controls namespace. Add the following to the top of you page's ASPX:

<%@ Register Tagprefix="SP" 
    Namespace="AssociatedIcons.Controls" Assembly="AssociatedIcons" %>

Now let's add some instances of our control in the page, like this:

<form id="TestPage" method="post" runat="server">
    <SP:AssociatedIcon id="file1" runat="server"
        ExtensionOrFile=".doc" largeIcon="True" />

    <SP:AssociatedIcon id="file2" runat="server"
        ExtensionOrFile="file.bmp" largeIcon="false" />

    <SP:AssociatedIcon id="Associatedicon1" runat="server"
        ExtensionOrFile="file.jpg" largeIcon="true" />

    <SP:AssociatedIcon id="Associatedicon2" runat="server"
        ExtensionOrFile="file.gif" largeIcon="false" />

    <SP:AssociatedIcon id="Associatedicon3" runat="server"
        ExtensionOrFile="file.mp3" largeIcon="true" />

    <asp:Button ID=test Runat=server  Text=test />

When you save, compile, and run this page, you should see something similar to the image below. The icons may vary depending on which programs you have associated with the file extensions.

Image 4

Enhancement suggestions

Like any sample, the code in this article is far from ideal and full of opportunities for new or enhanced features (maybe a few bugs here and there too). Some of the ideas that come to mind as enhancements to this code are:

  • Using a configuration value to determine both the physical and virtual paths of the icon image files. That way you can potentially use this control in more than one application and share the same set of icons, from the same folder.
  • Implementing the background transparency in GIF. Change the code to produce GIFs instead of PNGs (while IE does not get the transparent PNG bug fixed) and make the background color transparent. Here are two nice references that can help you with that: Transparent background using GDI+ by Eric Johnson (newsgroups post) and Bob Powell's GDI+ FAQ.


  • - I can't get around using the Win32 API without the help of this awesome Wiki and the nice people that contribute to it.
  • Bob Powell's GDI+ FAQ on GIF transparency.
  • Transparent background using GDI+ by Eric Johnson. If the link stops working, just search for the title in the microsoft.public.dotnet.framework.drawing newsgroup.


  • May 29th 2005 - Code submitted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United States United States
Sergio Pereira is a senior software developer and architect. He currently makes his services available throughout Chicago and suburbs. Sergio has always made himself proficient in mainstream Microsoft technologies and has a deep interest in emerging technologies and the software life cycle aspect of development.
Feel free to contact Sergio at sergio_pereira(AT)msn(dot)com.

Comments and Discussions

Questionsome errors on this Pin
pratik.p21-Feb-14 21:22
pratik.p21-Feb-14 21:22 
GeneralMy vote of 1 Pin
Manoj Gupta 00723-Jan-12 0:04
Manoj Gupta 00723-Jan-12 0:04 
GeneralFolder Icon Pin
scptech27-Dec-09 4:26
scptech27-Dec-09 4:26 
QuestionMemory leak? Pin
panchoandlefty24-Jan-09 14:11
panchoandlefty24-Jan-09 14:11 
AnswerRe: Memory leak? Pin
panchoandlefty24-Jan-09 14:35
panchoandlefty24-Jan-09 14:35 
GeneralWCSS in aspx page Pin
Member 392451114-Feb-08 23:59
Member 392451114-Feb-08 23:59 
GeneralCopyright Pin
simon w8-Jun-05 23:07
simon w8-Jun-05 23:07 
GeneralRe: Copyright Pin
Sergio Pereira9-Jun-05 11:20
Sergio Pereira9-Jun-05 11:20 
GeneralNice idea, but... Pin
Marcel VT30-May-05 0:18
Marcel VT30-May-05 0:18 
GeneralRe: Nice idea, but... Pin
Ashley van Gerven30-May-05 1:59
Ashley van Gerven30-May-05 1:59 
GeneralRe: Nice idea, but... Pin
Sergio Pereira31-May-05 5:16
Sergio Pereira31-May-05 5:16 
You are right, but how else would you be able to get those icons without manually extracting each of them (or maybe using a tool to extract the ones you want) ?
At least it should show familiar icons for the most common file types.
Of course, if you do not like the default icon displayed for a specific file type (unknown in the production server), you can always run this control in your dev box, get the ext.[32 or 16].XYZ.png file, and replace in production. It won't be overwritten.
Let us know if you have an alternative implementation.

Sergio Pereira
GeneralRe: Nice idea, but... Pin
Marcel VT31-May-05 6:26
Marcel VT31-May-05 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.