When StorageFile.GetScaledImageAsThumbnailAsync could fail?

Neo 421 Reputation points
2020-11-24T11:34:21.157+00:00

I am using StorageFile.GetScaledImageAsThumbnailAsync to get thumbnails of downloaded JPEG pictures (Bing daily's).

According to the doc, the method will return "null if there is no thumbnail image associated with the item". But for my tests, the method always returns correct thumbnails. I am wondering under what conditions would the method fail, so that I can handle the error with precaution.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2020-11-24T12:38:55.833+00:00

    Hello, @Neo Welcome to Microsoft Q&A,

    Derive from document, this method will return null if there is no thumbnail image associated with the item. But it hard to reproduce, because, if you can view the file in the file explorer, it means the file contains thumbnail image.

    And for your requirement, we suggest add null condition to notice user there is no thumbnail for current file and use try catch block to notice user exception message when getting file's thumbnail.

    try  
    {  
        var thumbnail = await file.GetScaledImageAsThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem);  
        if(thumbnail == null)  
        {  
            System.Diagnostics.Debug.WriteLine("----------thumbnail is null--------");  
        }  
        else  
        {  
      
        }  
    }  
    catch (Exception ex)  
    {  
      
        System.Diagnostics.Debug.WriteLine($"----------{ex.Message}--------");  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments