wpf - WPF中图像的淡入淡出

标签 wpf image slider slideshow

当我更改幻灯片放映等图像源时,如何实现 FadeIn 然后 FadeOut 图像。我的图像从本地和网络加载,并且数量是可变的。
谢谢

最佳答案

您可以编写一个扩展方法,通过将 Opacity 属性设置为 0 来淡出图像,然后设置 Source 属性,最后将不透明度设置为 1。

public static void ChangeSource(
    this Image image, ImageSource source, TimeSpan fadeOutTime, TimeSpan fadeInTime)
{
    var fadeInAnimation = new DoubleAnimation(1d, fadeInTime);

    if (image.Source != null)
    {
        var fadeOutAnimation = new DoubleAnimation(0d, fadeOutTime);

        fadeOutAnimation.Completed += (o, e) =>
        {
            image.Source = source;
            image.BeginAnimation(Image.OpacityProperty, fadeInAnimation);
        };

        image.BeginAnimation(Image.OpacityProperty, fadeOutAnimation);
    }
    else
    {
        image.Opacity = 0d;
        image.Source = source;
        image.BeginAnimation(Image.OpacityProperty, fadeInAnimation);
    }
}

关于wpf - WPF中图像的淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19706973/

相关文章:

c# - 如何为 Dialog 服务 MVVM Light 编写测试用例

image - 如何使用 MATLAB 获取文件夹中的所有图像?

javascript - 获取 SLICK SLIDER 的当前幻灯片并将类添加到内部元素

Javafx 自定义 slider 值

wpf - 如何将 WPF 样式应用于特定类型的所有元素?

c# - 如何覆盖数据网格列标题的自动创建?

css - 图像居中对齐

ruby - 如何使用 ImageMagick 创建图像?

jQuery:为幻灯片创建自动计时器 - 单击取消

c# - 如何从代码隐藏的用户控件中的内容控件中的数据模板中启动 Storyboard?