UWP gets shell icon for file

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

file thumbnails, image and text

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

shell file icon

Any thoughts?

thank

PS : I found a hack: create a file with 0 bytes with the same extension and create its thumbnail. Hopefully there is a better solution ...

+4
source share
1 answer

-, ( ):

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

:

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

All Articles