Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a Windows file icon in WPF?

Tags:

Currently I'm getting a native icon by calling SHGetFileInfo. Then, I'm converting it to a bitmap using the following code. The Bitmap eventually gets displayed in the WPF form.

Is there a faster way to do the same thing?

try         {             using (Icon i = Icon.FromHandle(shinfo.hIcon))             {                 Bitmap bmp = i.ToBitmap();                 MemoryStream strm = new MemoryStream();                 bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);                 BitmapImage bmpImage = new BitmapImage();                 bmpImage.BeginInit();                 strm.Seek(0, SeekOrigin.Begin);                 bmpImage.StreamSource = strm;                 bmpImage.EndInit();                  return bmpImage;             }         }         finally         {             Win32.DestroyIcon(hImgLarge);         } 
like image 325
Gautam Avatar asked Aug 25 '09 01:08

Gautam


People also ask

How to add an icon in WPF?

To add an icon to a project, right click on the project name in Solution Explorer. Right click will open Add Items menu. Select Add >> New Item. Now on Installed Templates, select Icon File (see Figure 1) and click Add.

What is the icon of file?

An ICON file contains image data for a computer icon used to represent files, folders, applications, and so on. ICON files are typically saved as small, square bitmap images, ranging from 16 x 16 to 256 x 256 pixels in size. Most Windows Icon files use the . ICO extension.


1 Answers

How about something like:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName); var bmp = icon.ToBitmap() 
like image 112
Craig Presti - MSFT Avatar answered Sep 21 '22 17:09

Craig Presti - MSFT