【发布时间】: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 图像的尺寸相当大。这可能会导致问题。如何设置AnimatedSource的DecodePixelWidth和DecodePixelHeight?
【问题讨论】:
-
出于好奇,您的 ImageStretchConverter 是否正在做任何使用 Image 的 StretchDirection 属性无法完成的事情?除此之外,它与您的问题几乎没有关系。
-
因此,当 Source 比 MaxThumbnailResolution 更宽或更高时,您似乎正在设置 UniformToFill。您也可以将 Image 的 MaxWidth 和 MaxHeight 设置为该值,然后(始终)将 UniformToFill 和 StretchDirection 设置为 DownOnly。
-
程序总是转到
return Stretch.None这一行。当我将其更改为Stretch.UniformToFill时,它只是调整了可观察图像的大小,但内存使用率仍然很高。 -
由于图片应该是GIF格式,所以一直在调用converter方法。
标签: wpf xaml animated-gif