WPF MediaElement中的动画gif会在几帧后停止动画

时间:2011-02-20 15:35:34

标签: wpf

我需要在WPF项目中显示一些媒体。媒体可以是jpeg,gif,png,bmp或wmv电影。所以我希望使用MediaElement进行diplaying。问题是我无法让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.gifhttp://www.gifanimations.com/GA/image/animations/aliens/alien-01.gif),直接从互联网和本地引用它们(已直接下载并链接到它们)磁盘上文件的路径),但在所有情况下,在几帧之后,gif停止动画。

我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试过从WPF MediaKit替换MediaElement?这可能会更好地满足您的需求。

相关问题