Animated gif within a WPF MediaElement stops animating after a few frames
I need to display some media in a WPF project. The media could be a jpeg, a gif, a png, a bmp or a wmv movie. So I'm hoping to use a MediaElement to do the diplaying. The problem is I can't get an animated gif to work properly.
I've seen a load of questions on here discussing how to display animated gifs within WPF applications, specifically:
1) Embed a windows forms Picture box within the app, which works ok, but the picture box doesn't scale properly when hosted within a ViewBox, so I can't use that
2) Create a custom GifImage which inherits from Image and do the animation myself. This works ok, but it means I need to worry about what type of media I am displaying, instead of just asking the element to deal with it (though I guess I could customise the object even further if I wanted to so it knew how to deal with all sorts of media). Also I think that some animated gifs only store the bit that has changed in each frame, so displaying the frame doesn't work properly - this one for instance: 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.The 3rd method is the one I'm trying to get working as it (ostensibly...) seems like the easiest method. However, whatever I do, I can't seem to get the animated gif to loop - in fact, it seems to get stuck after 3 frames. The code is below:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://sc开发者_如何学Gohemas.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>
I've tried the above with different animated gifs (these two: http://www.modernathlete.co.za/layout/banners/PEDI_Relax_469x60.gif and http://www.gifanimations.com/GA/image/animations/aliens/alien-01.gif), referencing them both directly from the internet, and locally (having downloaded and linked to them in a direct path to the file on the disk), but in all cases, after a few frames, the gif stops animating.
Am I doing something wrong here?
Have you tried the MediaElement replacement from the WPF MediaKit? That may work better for your needs.
精彩评论