How do I display a Windows file icon in WPF?

c#, icons, wpf

Solution

using System.Windows.Interop;

...

ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
    shinfo.hIcon,
    new Int32Rect(0,0,i.Width, i.Height),
    BitmapSizeOptions.FromEmptyOptions());

Problem

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); } ```

Original source

Related problems