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); }
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.
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.
How about something like:
var icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName); var bmp = icon.ToBitmap()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With