How to improve GetThumbnailAsync performance method in phone window 8.1

I am writing a function to display images in a folder (suppose I have about 60 images in this folder) in the phone window 8.1. And the problem is that the GetThumbnailAsync () function takes so long when I create a stream to get bitmapImage. Here is my code

//getFileInPicture is function get all file in picture folder List<StorageFile> lstPicture = await getFileInPicture(); foreach (var file in lstPicture) { var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50); var bitmapImage = new BitmapImage(); bitmapImage.DecodePixelWidth = (int)(ScreenWidth / 4); await bitmapImage.SetSourceAsync(thumbnail); g_listBitmapImage.Add(bitmapImage); //g_listBitmapImage is a list of bitmapImage } 

I was test and found a problem, the GetThumbnailAsync function takes so long . If I have about 60 images, it will take about 15 seconds to complete this function (I'm testing in lumia 730). Does anyone have this problem and how to make this code faster? .

Thanks so much for your support.

+1
source share
1 answer

You are currently expecting file.GetThumbnailAsync for each file, which means that although the function runs asynchronously for each file, it runs in order and not in parallel.

Try converting each async operation returned from file.GetThumbnailAsync to Task , and then save it to a list and then await all tasks using Task.WhenAll .

 List<StorageFile> lstPicture = await getFileInPicture(); List<Task<StorageItemThumbnail>> thumbnailOperations = List<Task<StorageItemThumbnail>>(); foreach (var file in lstPicture) { thumbnailOperations.Add(file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50).AsTask()); } // wait for all operations in parallel await Task.WhenAll(thumbnailOperations); foreach (var task in thumbnailOperations) { var thumbnail = task.Result; var bitmapImage = new BitmapImage(); bitmapImage.DecodePixelWidth = (int)(ScreenWidth / 4); await bitmapImage.SetSourceAsync(thumbnail); g_listBitmapImage.Add(bitmapImage); //g_listBitmapImage is a list of bitmapImage } 
+5
source

Source: https://habr.com/ru/post/981739/


All Articles