简体   繁体   中英

UWP get shell icon for a file

Most solutions here use System.Drawing which is not available in UWP afaik. GetThumbnailAsync() does the job but only for non-image files. With image files i always get a scaled preview, regardless of passed arguments.

文件缩略图,图像和文字

I'd like to have a shell file icon instead of a tiny preview to the left of the file name, like here:

shell文件图标

Any thoughts?

Thanks

PS : I have found a hack: create a 0 byte temp file with the same extension and make a thumbnail of it. I hope though there is a better solution...

Well here's a helper metod which uses the dummy file approach (place it in some static class):

public async static Task<StorageItemThumbnail> GetFileIcon(this StorageFile file, uint size = 32)
{
    StorageItemThumbnail iconTmb;
    var imgExt = new[] { "bmp", "gif", "jpeg", "jpg", "png" }.FirstOrDefault(ext => file.Path.ToLower().EndsWith(ext));
    if (imgExt != null)
    {
        var dummy = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("dummy." + imgExt, CreationCollisionOption.ReplaceExisting); //may overwrite existing
        iconTmb = await dummy.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
    }
    else
    {
        iconTmb = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
    }
    return iconTmb;
}

Usage example:

var icon = await file.GetFileIcon();
var img = new BitmapImage();
img.SetSource(icon);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM