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:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With