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

Cas*_*per 3 wpf xaml animated-gif

我想通过使用库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>
Run Code Online (Sandbox Code Playgroud)

图像拉伸转换器

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;
}
Run Code Online (Sandbox Code Playgroud)

原始 GIF 图像的大小相当高。这可能会导致问题。我怎样才能设置DecodePixelWidthDecodePixelHeightAnimatedSource

Llo*_*oyd 5

你读过这篇关于这个问题的文章吗? https://www.thomaslevesque.com/2015/01/17/a-new-library-to-display-animated-gifs-in-xaml-apps/

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

所有帧都在内存中预渲染,对于大 gif 来说这是一个大问题。可悲的是,在不重新编程的情况下使用相同的库时,确实没有简单的解决方法。

我建议尝试使用XamlAnimatedGif

https://github.com/thomaslevesque/XamlAnimatedGif

(由同一开发人员制作