簡體   English   中英

UWP獲取文件的shell圖標

[英]UWP get shell icon for a file

這里的大多數解決方案都使用System.Drawing,這在UWP afaik中是不可用的。 GetThumbnailAsync()完成工作,但僅適用於非圖像文件。 使用圖像文件,無論傳遞的參數如何,我都會獲得縮放預覽。

文件縮略圖,圖像和文字

我想要一個shell文件圖標,而不是文件名左邊的小預覽,如下所示:

shell文件圖標

有什么想法嗎?

謝謝

PS :我發現了一個hack:創建一個具有相同擴展名的0字節臨時文件並制作它的縮略圖。 我希望盡管有更好的解決方案......

那么這里是一個使用虛擬文件方法的輔助方法(將它放在一些靜態類中):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM