淡入淡出WPF中的图像

Beh*_*ian 4 wpf image slider slideshow

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

Cle*_*ens 13

您可以编写一个扩展方法,通过将其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);
    }
}
Run Code Online (Sandbox Code Playgroud)