I can’t run an animated gif on my WPF project

Asked

Viewed 498 times

2

I downloaded a gif. link. The problem that the link gives a double click and works. Drag to my application and no longer works. The image appears, but stopped and not moving. I want to put this gif instead of a progressibar.

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

zipgif.Visibility = System.Windows.Visibility.Visible;
  • Your link is already invalid. Other gifs work?

  • @Rodrigoguiotti, I just tested this one. The thing is, I don’t know how to work with animated gif. I have to do something in the code or in the shampoo?

  • you can do it both ways, but in the shampoo you have to download a package...

  • I found here the name: Wpfanimatedgif

  • @Rodrigoguiotti, man, you know where I can find a T-shirt that’ll teach me to trample with this guy?

  • only in English... then when I have more time, I can even translate more things from there... that’s why I haven’t put together an answer now: https://wpfanimatedgif.codeplex.com/documentation

Show 1 more comment

2 answers

1

Hello, I was looking for information because I was going through 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 Gif to enter 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 small explanation for the code: the Source path to 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 excerpt x:Name="mediaElement"

As for the properties HorizontalAlignment="Center" Height="200" Margin="0" VerticalAlignment="Center" Width="200" is something personal to you.

I hope I was able to help you. Hug.

1

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

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

<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 • Using the library Wpfanimatedgif that you can download and reference in your project through Nuget

PM> Install-Package WpfAnimatedGif

To use simply 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" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.