Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Get Icon for File Path as a .NET Web Image
To populate a web image with the icon for a file path, you need a few things.
First, get the raw Icon that is the image for the file. This is shown in a related post where I document the method "FileIconLoader.GetFileIcon"
Post: [Get Icon for Windows File Type Extension - C#]

Then, create an ashx handler to stream that image to the web.

Then, set your Image Url/Src to the handler.

Here is the code for the handler. An example URL is:
FileIconHandler.ashx?ext=.jpg


///
/// Summary description for FileIconHandler
///

public class FileIconHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
lock (typeof(FileIconHandler))
{ // GetFileIcon doesn't support multiple threads
string fn = HttpContext.Current.Request[QueryParams.Q_FILE_EXTENSION];
var icon = FileIconLoader.GetFileIcon("test" + fn, false);
if (icon != null)
{
using (Bitmap bmp = icon.ToBitmap())
{
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(ms.ToArray());
}
}
}
}
}
catch
{
}
}

public bool IsReusable
{
get { return false; }
}
} // class

Created By: amos 8/22/2013 11:41:42 AM
Updated: 8/22/2013 12:11:19 PM