Behzad Ashrafian
Behzad Ashrafian

Reputation: 267

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

Upvotes: 4

Views: 6817

Answers (2)

Clemens
Clemens

Reputation: 128061

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);
    }
}

Upvotes: 15

Firoz
Firoz

Reputation: 7454

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

Upvotes: 0

Related Questions