WPF 动画 GIF 使用太多内存来显示大 GIF 图像

WPF Animated GIF Use Too Much Memory for Displaying Large GIF Image

我想使用库 WPF Animated GIF 显示 GIF。但是设置属性PictureSource后,进程内存从208MB上升到1GB。为什么?

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

原始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

(由同一开发者制作)