【问题标题】:How to improve performance method GetThumbnailAsync in window phone 8.1如何在 window phone 8.1 中提高性能方法 GetThumbnailAsync
【发布时间】:2015-05-31 08:31:45
【问题描述】:

我在 windows phone 8.1 中编写了一个函数来显示文件夹上的图像(假设我在这个文件夹中有大约 60 张图像)。问题是当我创建流以获取位图图像时,函数 GetThumbnailAsync() 需要很长时间。 这是我的代码

       //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
        }

我进行了测试,发现问题是函数 GetThumbnailAsync 需要很长时间。 如果我有大约 60 张图片,完成此功能大约需要 15 秒(我在 lumia 730 中测试)。 有人遇到这个问题吗?如何让这段代码运行得更快?

非常感谢您的支持

【问题讨论】:

    标签: c# image-processing optimization winrt-xaml


    【解决方案1】:

    您当前正在等待每个文件的file.GetThumbnailAsync,这意味着虽然该函数是针对每个文件异步执行的,但它是按顺序执行的,而不是并行执行的。

    尝试将从file.GetThumbnailAsync返回的每个异步操作转换为Task,然后将它们存储在列表中,然后使用Task.WhenAll将所有任务存储在await

    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
       }
    

    【讨论】:

    • 您好,非常感谢您的帮助,但是这个函数 thumbnailOperations.Add(file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,50)) 有错误。 file.getThumbnailAsync() 有 IAsyncOperation 类型,使 thumbnailOperations 无法添加。
    • @PhamLuc 看起来回答者打错了:将.StartAsTask() 放在括号的内部,紧跟在.GetThumbnailAsync() 之后。
    • @James Ko ,对不起,file.GetThumbnailAsync() 没有函数 .StartAsTask(),所以不能像你说的那样......
    • @PhamLuc 尝试删除.StartAsTask(),在它旁边放一个点,看看自动补全中是否有任何方法表明它可以将其转换为Task(即.AsTask())。如果这不起作用,请尝试在 this 类中添加 using 语句,因为那里有将 IAsyncOperation 转换为 Task 的扩展方法。
    • @James Ko,非常感谢。添加 .AsTask() 后,代码可以在 Visual Studio 2013 的调试模式下运行。它的速度超过了大约 5 秒。但如果我在没有 Visual Studio 2013 的情况下运行,它会崩溃。你能加我的Skype昵称吗?或给我您的电子邮件,我将发送我的代码供您审核。我的 Skype 邮件:thichchoi_honhoc@yahoo.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多