3

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://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>

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?

Community
  • 1
  • 1
MajorRefactoring
  • 3,713
  • 3
  • 20
  • 26
  • I can indeed reproduce the issue using the GIF files you provided. It looks like the problem comes from the file size, because I initially made that work with a small spinner image (~2KB). However, it seems that the `MediaElement` solution is not reliable, so I'll update my answer to the initial question accordingly. – Frédéric Hamidi Feb 22 '11 at 09:15

1 Answers1

0

Have you tried the MediaElement replacement from the WPF MediaKit? That may work better for your needs.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • It sure does! Got it working by loading the gif file from local disk, now just need to get it to load direct from the internet (which I'll either do by grabbing it and downloading it or figure out how to do it direct in the MediaUriElement). Thanks very much for this! – MajorRefactoring Mar 26 '11 at 23:13
  • @MajorRefactoring: I meet the same problem, but it seems WPF MediaKet:MediaUriElement still not solve it.Do you know any example for this issue? – Hoàng Long Dec 08 '11 at 14:47
  • Sorry to hear it's not working for you Hoàng. I'm afraid it worked fine for me, so unless you give me some more details I won't be able to suggest any things you might look in to. – MajorRefactoring Dec 08 '11 at 19:55