【问题标题】:Load a series of images that have the effect of a movement WPF C#加载一系列具有运动效果的图像 WPF C#
【发布时间】:2024-01-18 11:48:01
【问题描述】:

我想知道如何制作一个按钮来加载一个允许我上传 3 张图像的窗口,从而给人一种 gif 的印象。

如果我只做一个项目,一切都很好,但是当我这样做时,我关联的按钮对我不起作用。 这是为什么? 谁能帮我理解一下?

此代码 XAML:

<Image Name="ConnectionImage" Grid.RowSpan="2" Grid.ColumnSpan="2">
            <Image.Triggers>
                <EventTrigger RoutedEvent="Image.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Name="DoubleAnimation_ConnectionTest" Storyboard.TargetName="ConnectionImage" 
                                             Storyboard.TargetProperty="Width" From="200" To="200" 
                                             Duration="0:0:1.0"  Completed="DoubleAnimation_Completed"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Image.Triggers>
        </Image>

而图像的代码是:

    int ctr = 1;
    private void DoubleAnimation_Completed (object sender, EventArgs e)
    {
        ShowImage();
        DoubleAnimation_ConnectionTest.BeginAnimation(Image.WidthProperty, DoubleAnimation_ConnectionTest);
    }

    private void ShowImage()
    {
        string filename = "Images/Connection" + ctr + ".jpg";
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.UriSource = new Uri(filename, UriKind.Relative);
        image.EndInit();
        ConnectionImage.Source = image;

        ctr++;

        if (ctr > 3)
        {
            ctr = 1;
        }
    }

和按钮:

 private void Button_Test_DB_Click(object sender, RoutedEventArgs e)
        {
            Window_Test Test_DB = new Window_Test();
            Test_DB.Show();

        }

【问题讨论】:

  • “对我不起作用”并不是一个真正有用的问题描述。请更具体地说明这一点。并注意:使用动画的 Completed 事件循环执行某些代码看起来真的很奇怪。为什么不直接使用 DispatcherTimer?
  • 如果我在一个窗口中做一个只有动画的项目,那么它可以工作,否则从窗口中检索按钮,我会得到一个空的窗口。

标签: c# wpf image load


【解决方案1】:

使用 C# 完全可以做到这一点。您可以使用FrameDimension Class 来帮助您。不幸的是,我不能因为重复这个问题而关闭这个问题,因为这些帖子没有可接受的答案......相反,我只能建议您在 Stack Overflow 上查看How do I get an animated gif to work in WPF?Animating Gif in WPF 问题的答案.

此外,您还可以在 Visual C# Kicks 网站上看到 The Code - An Animated GIF Class 页面。

【讨论】:

  • 谢谢,但它真的不是 gif,而是一系列动画图像,因为它们是按顺序排列的。
最近更新 更多