【问题标题】:How to add a gif animation in a wpf page of class library project? [duplicate]如何在类库项目的wpf页面中添加gif动画? [复制]
【发布时间】:2018-12-11 19:54:05
【问题描述】:

我是创建 revit 插件和使用类库项目的新手。目前,我正在使用类库项目创建插件,第一个屏幕是登录屏幕(为此使用 wpf),我想向该 wpf 页面添加 gif 动画。我在网上搜索并找到了一些解决方案(wpfanimatedgif nuget 包、mediaelement),但 gif 无法在 revit 中播放。所以我在 WPF 项目而不是类库项目中尝试了这些解决方案。他们工作了。有人可以帮帮我吗?是否有另一个可用的 nuget 包? 谢谢

编辑:- 里面 C# code ,当我使用媒体元素时,它确实显示了 GIF 的缩略图。

但在里面 Revit ,即使是 GIF 的缩略图也不可见

在 Xaml 中:

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

在后面的代码中:-

private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            My_GIF.Position = new TimeSpan(0, 0, 20);
            My_GIF.Play();
            //MessageBox.Show("Playing GIF");
        }

【问题讨论】:

  • 欢迎来到stackoverflow。请花一分钟时间回答tour,尤其是How to Ask,以及edit您的问题。

标签: c# wpf class-library revit-api revit


【解决方案1】:

我只是在 WPF 中使用图像而不是媒体元素,因为使用媒体元素我的动画在完成动画之前一直停止。所以现在我只是将它逐帧输入到图像中。你也可以像这样更好地控制它。

XAML:

<Image x:Name="image"/>

代码隐藏(在 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();

这会播放一次 gif 并在最后一个帧之前的帧处停止(我的 gif 文件末尾有一个空白帧)。如果你想让它重复,你可以把'for'循环放在一个while循环或你喜欢的其他循环中。 您可以通过将Thread.Sleep(50) 时间设置为其他值来调整速度。

另外,请务必在其属性中将您的图像设置为“资源”,并且您可能必须在 Uri 中像这样引用它:"pack://application:,,,/&lt;ProjectName&gt;;component/Images/planBIMGIF.gif" &lt;ProjectName&gt; 是您在 VS 中的项目名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2017-08-05
    • 2010-12-05
    • 2016-12-28
    • 2011-08-15
    • 2022-01-14
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多