【问题标题】:WPF Animated GIF Use Too Much Memory for Displaying Large GIF ImageWPF 动画 GIF 使用太多内存来显示大型 GIF 图像
【发布时间】:2018-01-27 20:21:32
【问题描述】:

我想通过使用库 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

【问题讨论】:

  • 出于好奇,您的 ImageStretchConverter 是否正在做任何使用 Image 的 StretchDirection 属性无法完成的事情?除此之外,它与您的问题几乎没有关系。
  • 因此,当 Source 比 MaxThumbnailResolution 更宽或更高时,您似乎正在设置 UniformToFill。您也可以将 Image 的 MaxWidth 和 MaxHeight 设置为该值,然后(始终)将 UniformToFill 和 StretchDirection 设置为 DownOnly。
  • 程序总是转到return Stretch.None这一行。当我将其更改为Stretch.UniformToFill 时,它只是调整了可观察图像的大小,但内存使用率仍然很高。
  • 由于图片应该是GIF格式,所以一直在调用converter方法。

标签: wpf xaml animated-gif


【解决方案1】:

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

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

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

我建议尝试使用 XamlAnimatedGif

https://github.com/thomaslevesque/XamlAnimatedGif

(由同一个开发者制作)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 2011-10-27
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2020-08-30
    • 2011-02-09
    • 1970-01-01
    相关资源
    最近更新 更多