I can not make an animated gif work in my WPF project

2

I downloaded a gif.animado in this link . The problem that in the link I double click and it works. I drag it to my application and it no longer works. The image appears, but still and not moving. I want to put this gif instead of a progressbar.

I got it in part. It worked. However, when I start it with Hidden and in the code I have it appear, it is shown, but without movement. It looks like a normal picture.

zipgif.Visibility = System.Windows.Visibility.Visible;
    
asked by anonymous 18.03.2016 / 17:11

2 answers

1

Hello, I was looking for information because I had the same problem, and in the end I found your question right after I found an answer (rs).

Well, to put a Gif in a WPF window, I used a <MediaElement> . In my case, something like this:

<MediaElement x:Name="mediaElement" MediaEnded="mediaElement_MediaEnded" HorizontalAlignment="Center" Height="200" Margin="0" VerticalAlignment="Center" Width="200" Source="CaminhoAbsolutoPara/Gif.gif" Stretch="Fill" UnloadedBehavior="Manual" LoadedBehavior="Play"/>

And then I created an event for the Gif to go into a loop. In the MediaEnded event I put a code like this:

private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
    {
        mediaElement.Position = new TimeSpan(0, 0, 1);
        mediaElement.Play();
    }

Well, just a little explanation for the code: the Source path to the Gif should be absolute. For example: C: //...image.gif

In the event, the name mediaElement.Position ... and mediaElement.Play , is the name you gave to your MediaElement in the x:Name="mediaElement"

Since the HorizontalAlignment="Center" Height="200" Margin="0" VerticalAlignment="Center" Width="200" properties are something personal to you.

I hope I have been able to help you. Thanks.

    
27.05.2016 / 18:57
1

By default WPF does not offer any specific controls for running Gif image files. But there are two simple ways to solve this problem:

1 • Attach the Gif to the MediaElement control. Yes, this component can run the Gif, but it will not loop, you have to do this manually, and for this you must root the MediaEnded event in it, reset the Position and then call the Play method.

<MediaElement x:Name="Me" MediaEnded="MeMediaEnded" Source="meugif.gif" UnloadedBehavior="Manual" LoadedBehavior="Play"/>

private void MeMediaEnded(object sender, RoutedEventArgs e)
{
    //Reseta a posição.
    Me.Position = new TimeSpan(0, 0, 1);

    //Executa novamente.
    Me.Play();
}

2 • Use the WpfAnimatedGif library that you can download and reference in your project through NuGet

PM> Install-Package WpfAnimatedGif

To use just add the namespace to your Xaml file and attach the properties provided by the library. See below:

<Window x:Class="WpfGif.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:gif="http://wpfanimatedgif.codeplex.com"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Image gif:ImageBehavior.AnimatedSource="Images/meugif.gif" />
    
26.10.2017 / 23:54