wpf WPF中图像的淡入淡出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19706973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:59:28  来源:igfitidea点击:

Fade In-Out for image in WPF

wpfimagesliderslideshow

提问by Behzad Ashrafian

How can i implement FadeIn and then FadeOut image when i change image source like slide show. my images load from local and web and count of that is varaible. Thankyou

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

回答by Clemens

You could write an extension method that fades out the image by animating its Opacityproperty to 0, then sets the Sourceproperty and finally animates the opacity back to 1.

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

回答by Firoz

You can use WPF transition for this, please check at Here

您可以为此使用 WPF 过渡,请在此处查看