【发布时间】:2011-02-20 15:35:34
【问题描述】:
我需要在 WPF 项目中显示一些媒体。媒体可以是 jpeg、gif、png、bmp 或 wmv 电影。所以我希望使用 MediaElement 来进行显示。问题是我无法让动画 gif 正常工作。
我在这里看到了很多关于如何在 WPF 应用程序中显示动画 gif 的问题,具体来说:
1) Embed a windows forms Picture box within the app,工作正常,但图片框在 ViewBox 中托管时无法正确缩放,所以我不能使用它
2)Create a custom GifImage which inherits from Image and do the animation myself。这工作正常,但这意味着我需要担心我正在显示什么类型的媒体,而不是仅仅要求元素处理它(尽管我想如果我想我可以进一步自定义对象,所以它知道如何处理各种媒体)。另外我认为一些动画 gif 只存储每一帧中发生变化的位,因此显示帧不能正常工作 - 例如:http://www.modernathlete.co.za/layout/banners/PEDI_Relax_469x60.gif)
3)Use a MediaElement and set up a trigger with a storyboard to loop the gif infinitely.
第三种方法是我正在尝试使用的方法,因为它(表面上......)似乎是最简单的方法。但是,无论我做什么,我似乎都无法让动画 gif 循环播放 - 事实上,它似乎在 3 帧后卡住了。代码如下:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<MediaElement Name="yourMediaElement">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline Source="http://www.modernathlete.co.za/layout/banners/PEDI_Relax_469x60.gif"
Storyboard.TargetName="yourMediaElement"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
</Grid>
</Window>
我已经用不同的动画 gif(这两个:http://www.modernathlete.co.za/layout/banners/PEDI_Relax_469x60.gif 和 http://www.gifanimations.com/GA/image/animations/aliens/alien-01.gif)尝试了上述方法,直接从 Internet 和本地引用它们(已下载并链接到文件的直接路径中)在磁盘上),但在所有情况下,几帧之后,gif 就会停止动画。
我在这里做错了吗?
【问题讨论】:
-
我确实可以使用您提供的 GIF 文件重现该问题。看起来问题出在文件大小上,因为我最初使用一个小的微调器图像(~2KB)。但是,
MediaElement的解决方案似乎并不可靠,因此我将相应地更新对初始问题的回答。
标签: wpf