目前,我正在通过调用SHGetFileInfo获得 native 图标。然后,我使用以下代码将其转换为位图。该位图最终以WPF形式显示。

有没有更快的方法来做同样的事情?

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

最佳答案

using System.Windows.Interop;

...

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

10-06 00:35