WPF 动画 GIF 使用太多内存来显示大 GIF 图像 [英] WPF Animated GIF Use Too Much Memory for Displaying Large GIF Image

查看:49
本文介绍了WPF 动画 GIF 使用太多内存来显示大 GIF 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用库

XAML

<图像.拉伸><MultiBinding Converter="{StaticResource ImageStretchConverter}"><绑定路径=图片来源"/><Binding ElementName="content" Path="Source.Width"/><Binding ElementName="content" Path="Source.Height"/></多重绑定></Image.Stretch><Image.BitmapEffect><BlurBitmapEffect Radius="0"/></Image.BitmapEffect><Image.CacheMode><BitmapCache EnableClearType="True"RenderAtScale="0.2"SnapsToDevicePixels="True"/></Image.CacheMode><!--<Image.Source><BitmapImage StreamSource="{Binding Path=PictureSource}" UriSource="{Binding Path=PictureSource}"DecodePixelWidth="340" DecodePixelHeight="240"/></Image.Source>--></图片>

ImageStretchConverter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {字符串路径 = values[0] 作为字符串;if (string.IsNullOrEmpty(path) || values[1] == DependencyProperty.UnsetValue || values[2] == DependencyProperty.UnsetValue) {返回 Stretch.None;}if (Path.GetExtension(path).ToLower() == ".gif") {双倍宽度=(双)值[1];双高度=(双)值[2];if (width > Configuration.MaxThumbnailResolution || height > Configuration.MaxThumbnailResolution) {返回 Stretch.UniformToFill;}}返回 Stretch.None;}

原始 GIF 图片的尺寸相当大.这可能会导致问题.如何设置 AnimatedSourceDecodePixelWidthDecodePixelHeight?

解决方案

您是否阅读了有关该问题的这篇文章?https://www.thomaslevesque.com/2015/01/17/a-new-library-to-display-animated-gifs-in-xaml-apps/

该问题很可能是由开发人员在创建库时犯的错误引起的.您可以在文章中阅读所有相关内容,但仅作简短说明.

所有帧都在内存中预渲染,对于大 gif 来说这是一个大问题.可悲的是,如果使用相同的库而不对其进行重新编程,确实没有简单的解决方法.

我建议尝试使用 XamlAnimatedGif.

https://github.com/thomaslevesque/XamlAnimatedGif

(由同一开发者制作)

I want to display a GIF by using a library WPF Animated GIF. But when the property PictureSource is set, the process memory rises from 208MB to 1GB. Why?

XAML

<Image Name="content" MaxHeight="240" MaxWidth="340" 
       RenderOptions.BitmapScalingMode="LowQuality"
       Width="340" Height="240"
       MinWidth="340" MinHeight="240"
       gif:ImageBehavior.AutoStart="True"
       gif:ImageBehavior.AnimatedSource="{Binding Path=PictureSource}">
        <Image.Stretch>
            <MultiBinding Converter="{StaticResource ImageStretchConverter}">
                <Binding Path="PictureSource" />
                <Binding ElementName="content" Path="Source.Width" />
                <Binding ElementName="content" Path="Source.Height" />
            </MultiBinding>
        </Image.Stretch>
        <Image.BitmapEffect>
            <BlurBitmapEffect Radius="0" />
        </Image.BitmapEffect>
    <Image.CacheMode>
        <BitmapCache EnableClearType="True" 
                RenderAtScale="0.2" 
                SnapsToDevicePixels="True"/>
    </Image.CacheMode>
    <!--<Image.Source>
        <BitmapImage StreamSource="{Binding Path=PictureSource}" UriSource="{Binding Path=PictureSource}"
            DecodePixelWidth="340" DecodePixelHeight="240"/>
    </Image.Source>-->
</Image>

ImageStretchConverter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
    string path = values[0] as string;
    if (string.IsNullOrEmpty(path) || values[1] == DependencyProperty.UnsetValue || values[2] == DependencyProperty.UnsetValue) {
        return Stretch.None;
    }

    if (Path.GetExtension(path).ToLower() == ".gif") {
        double width = (double)values[1];
        double height = (double)values[2];
        if (width > Configuration.MaxThumbnailResolution || height > Configuration.MaxThumbnailResolution) {
            return Stretch.UniformToFill;
        }
    }

    return Stretch.None;
}

The size of the original GIF image is quite high. This may cause the problem. How can I set the DecodePixelWidth and DecodePixelHeight of the AnimatedSource?

解决方案

Have you read this article on the problem? https://www.thomaslevesque.com/2015/01/17/a-new-library-to-display-animated-gifs-in-xaml-apps/

The problem is most likely caused by a mistake the developer made when creating the library. You can read all about it in the article, but for a short explanation.

All the frames are pre-rendered in memory, for large gifs this is a big problem. Sadly enough there really isn't a easy fix for that when using the same library without re-programming it.

I'd recommend trying to use XamlAnimatedGif.

https://github.com/thomaslevesque/XamlAnimatedGif

(Which was made by the Same Developer)

这篇关于WPF 动画 GIF 使用太多内存来显示大 GIF 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆