Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

How about something like:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName);
var bmp = icon.ToBitmap()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...