c# - UWP 应用程序更快地加载图像?

标签 c# image performance win-universal-app

我正在为通用 Windows 平台编写一个应用程序,我需要在其中加载一些图像以显示在列表中。现在我的问题是这种加载对用户来说花费的时间太长了。我目前使用绑定(bind)在 XAML 中设置图像源,如下所示:{Binding Image}

我只能访问全分辨率图像,所以我想知道是否有办法在将这些图像放在 UI 上之前将它们缩小,因此只有较小的图像需要保留在内存中。有没有一种方法可以配置 Image UI 元素,使其能够自行调整大小?

还有,有没有办法延迟加载这些图像,因为现在我的 UI 被加载这些图像阻塞了。

编辑:我用来将本地镜像加载到我的 Image 中的代码(在 ListView.ItemTemplate 中):

<Image
    Grid.Row="0"
    Source="{Binding Image}"
    Stretch="Uniform"
    VerticalAlignment="Center"/>

最佳答案

您可以使用转换器延迟加载图像和调整图像大小。 In 是您可以使用的转换器的开始。有了这个,延迟加载就为你完成了。但是,我没有准备好调整大小的示例。

转换器代码

class LoadAttachmentAsyncConverter : IValueConverter
{
    public override object Convert(object value, Type targetType, object parameter, string language)
    {
        Task<BitmapImage> taskToExecute = GetImage(<some parameter>);
        //Possibly handle some other business logic
        return new NotifyTaskCompletion<BitmapImage>(taskToExecute);
    }

    public async Task<BitmapImage> GetImage(object someParameter) {
        BitmapImage image = new BitmapImage();
        //do (async stuff) to fill the image;
        return image;
    }
}

XAML 代码

<Image Source="{Binding Result}" DataContext="{Binding converterObjValue, Converter={StaticResource ConverterName}}"/>

有关实现调整大小的信息,您可以在此处找到信息:https://social.msdn.microsoft.com/Forums/en-US/490b9c01-db4b-434f-8aff-d5c495e67e55/how-to-crop-an-image-using-bitmaptransform?forum=winappswithcsharp

关于c# - UWP 应用程序更快地加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885188/

相关文章:

c# - .ToUniversalTime(),为什么会这样?

android - 如何在 Android 上访问位图图像的像素?

c# - 打开和关闭连接的性能与 commandTimeout

java - 通过重用Document和Field实例来提高Lucene索引性能时出现的问题

c# - 如何将参数和上传图片同时传递给 One web api?

c# - 在应用程序状态中存储通用列表

android - 在不重新分配目标对象的情况下裁剪图像

performance - 使用 -g0 编译的代码是否比 -g3 更快?

c# - 类型定义和类型引用有什么区别?

java - Netbeans 导致我的 Logo 颜色反转?