0

I am new to creating revit addins and using class library projects. Currently, I am creating an addin using a class library project and the first screen is a login screen(using wpf for this) and I want to add a gif animation to that wpf page. I have searched online and found some solutions (wpfanimatedgif nuget package, mediaelement) but the gif won't play inside revit. So I tried these solutions in a WPF project instead of class library project. And they worked. Can someone please help me? Is there another nuget package available? Thanks

EDIT:- Inside C# code , it does shows the thumbnail of the GIF when I use the media element.

But inside Revit , even the thumbnail for the GIF is not visible

In the Xaml:

<MediaElement Name="My_GIF" LoadedBehavior="Play" UnloadedBehavior="Manual" MediaEnded="MediaElement_MediaEnded" Source="Images\planBIMGIF.gif"/>

In the code behind:-

private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            My_GIF.Position = new TimeSpan(0, 0, 20);
            My_GIF.Play();
            //MessageBox.Show("Playing GIF");
        }
  • Welcome to stackoverflow. Please take a minute to take the [tour], especially [ask], and [edit] your question accordingly. – jazb Dec 11 '18 at 06:32

1 Answers1

0

I just use an image in WPF instead of a media element because with a media element my animations keep stopping before finishing the animation. So now I just feed it to the image frame by frame. You also have more control over it like this.

XAML:

<Image x:Name="image"/>

Code-behind (in a function that gets called after InitializeComponent):

Uri myUri = new Uri("Images\planBIMGIF.gif", UriKind.RelativeOrAbsolute);
GifBitmapDecoder decoder = new GifBitmapDecoder(myUri,       
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource;
int frameCount = decoder.Frames.Count;

Thread th = new Thread(() =>
{
    for (int i = 0; i < frameCount - 1; i++)
    {
        this.Dispatcher.Invoke(new Action(delegate ()
        {
            bitmapSource = decoder.Frames[i];
            image.Source = bitmapSource;
        }));
        System.Threading.Thread.Sleep(50);
    }
});
th.Start();

This plays the gif once and stops at the frame before the last one (my gifs had a blank frame at the end). If you want it to repeat, you can put the 'for' loop in a while loop or some other loop of your preference. You can adjust the speed by setting the Thread.Sleep(50) time to something else.

Also, be sure to set your image as "resource" in its properties, and you might have to reference it like this in the Uri: "pack://application:,,,/<ProjectName>;component/Images/planBIMGIF.gif" with <ProjectName> being the name of your project in VS.

Jan Buijs
  • 98
  • 1
  • 6