代码之家  ›  专栏  ›  技术社区  ›  paradisonoir

在wpf中显示动画gif

  •  10
  • paradisonoir  · 技术社区  · 15 年前

    我想显示一个动画gif,如加载…在我的xaml中,因为我的程序正在进行中。我发现在wpf中这并不容易,因为我加载了gif,它只显示了第一帧。在wpf中显示动画的最佳方法是什么?

    3 回复  |  直到 7 年前
        1
  •  5
  •   CodeMouse92    13 年前

    我有这个问题,直到我发现在WPF4中,你可以模拟你自己的关键帧图像动画。首先,将动画分割成一系列图像,将它们命名为“image1.gif”、“image2,gif”等。将这些图像导入到解决方案资源中。我假设你把它们放在图片的默认资源位置。

    你将使用图像控件。使用下面的xaml代码。我把非必需品都拿走了。

    <Image Name="Image1">
       <Image.Triggers>
          <EventTrigger RoutedEvent="Image.Loaded"
             <EventTrigger.Actions>
                <BeginStoryboard>
                   <Storyboard>
                       <ObjectAnimationUsingKeyFrames Duration="0:0:1" Storyboard.TargetProperty="Source" RepeatBehavior="Forever">
                          <DiscreteObjectKeyFrames KeyTime="0:0:0">
                             <DiscreteObjectKeyFrame.Value>
                                <BitmapImage UriSource="Images/Image1.gif"/>
                             </DiscreteObjectKeyFrame.Value>
                          </DiscreteObjectKeyFrames>
                         <DiscreteObjectKeyFrames KeyTime="0:0:0.25">
                            <DiscreteObjectKeyFrame.Value>
                               <BitmapImage UriSource="Images/Image2.gif"/>
                            </DiscreteObjectKeyFrame.Value>
                         </DiscreteObjectKeyFrames>
                         <DiscreteObjectKeyFrames KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                               <BitmapImage UriSource="Images/Image3.gif"/>
                            </DiscreteObjectKeyFrame.Value>
                         </DiscreteObjectKeyFrames>
                         <DiscreteObjectKeyFrames KeyTime="0:0:0.75">
                            <DiscreteObjectKeyFrame.Value>
                               <BitmapImage UriSource="Images/Image4.gif"/>
                            </DiscreteObjectKeyFrame.Value>
                         </DiscreteObjectKeyFrames>
                         <DiscreteObjectKeyFrames KeyTime="0:0:1">
                            <DiscreteObjectKeyFrame.Value>
                               <BitmapImage UriSource="Images/Image5.gif"/>
                            </DiscreteObjectKeyFrame.Value>
                         </DiscreteObjectKeyFrames>
                      </ObjectAnimationUsingKeyFrames>
                   </Storyboard>
                </BeginStoryboard>
             </EventTrigger.Actions>
          </EventTrigger>
       </Image.Triggers>
    </Image>
    
        2
  •  1
  •   Matt Lacey    15 年前

    你可以嵌入一个medialElement

    <MediaElement LoadedBehavior="Play" Source="path/to.file" />
    

    或Winforms PictureBox:

        <wfi:WindowsFormsHost>
            <winForms:PictureBox x:Name="pictureBoxLoading">
            </winForms:PictureBox>
        </wfi:WindowsFormsHost>
    

    不过,我建议在wpf中找到一种方法。看看故事板和动画。如果你不知道你想达到什么目标,也不知道你为什么要这么做,很难给出进一步的建议。

        3
  •  0
  •   Athena Vinod R    7 年前

    只需右键单击.gif文件并更改两个属性:

    生成操作:嵌入资源

    复制到输出目录:如果更新,则复制

    然后

     <MediaElement x:Name="myGif" UnloadedBehavior="Manual" Source="giphy_s.gif" MediaEnded="MediaElement_MediaEnded"/>
    

    并设置事件以继续运行

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