Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

file thumbnails, image and text

I'd like 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?

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...

like image 521
Mike Tsayper Avatar asked May 25 '16 05:05

Mike Tsayper


1 Answers

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);
like image 74
Mike Tsayper Avatar answered Nov 05 '22 08:11

Mike Tsayper