Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade In-Out for image in WPF

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

like image 350
Behzad Ashrafian Avatar asked Dec 12 '22 10:12

Behzad Ashrafian


1 Answers

You could write an extension method that fades out the image by animating its Opacity property to 0, then sets the Source property and finally animates the opacity back to 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);
    }
}
like image 179
Clemens Avatar answered Dec 27 '22 12:12

Clemens