问题
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
?
回答1:
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)
来源:https://stackoverflow.com/questions/45768644/wpf-animated-gif-use-too-much-memory-for-displaying-large-gif-image