图像过渡

时间:2011-01-12 02:36:05

标签: c# wpf image transition storyboard

我再次陷入困境。

我正在用C#创建一个WPF项目......

我有一张图片(专辑封面),我将在代码背后更改,并希望基本完成以下操作。

当确定并获取新专辑封面图像时,我希望图像控件中的当前图像淡出,使用新封面更新,然后淡入。

我没有看到很多关于如何在后面的代码中完成此任务的好例子。

以下是我最近的失败尝试......

if (currentTrack != previousTrack) 
{
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    image.UriSource = new Uri(Address, UriKind.Absolute);
    image.EndInit();

    Storyboard MyStoryboard = new Storyboard();

    DoubleAnimation FadeOut = new DoubleAnimation();
    FadeOut.From = 1.0;
    FadeOut.To = 0.0;
    FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));

    MyStoryboard.Children.Add(FadeOut);
    Storyboard.SetTargetName(FadeOut, CoverArt.Name);
    Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Rectangle.OpacityProperty));

    CoverArt.Source = image;

    DoubleAnimation Fadein = new DoubleAnimation();
    Fadein.From = 0.0;
    Fadein.To = 1.0;
    Fadein.Duration = new Duration(TimeSpan.FromSeconds(.5));

    MyStoryboard.Children.Add(Fadein);
    Storyboard.SetTargetName(Fadein, CoverArt.Name);
    Storyboard.SetTargetProperty(Fadein, new PropertyPath(Rectangle.OpacityProperty));

    MyStoryboard.Begin(this);
}

我更喜欢在代码中执行此操作,因为这是我在查询图像的地方。否则,我不确定我是如何触发的。

非常感谢一个例子。

2 个答案:

答案 0 :(得分:1)

如果您希望旧图像淡出然后新图像淡入使用相同的图像目标,则必须等到第一个故事板完成才能更改图像并启动新图像。为此,请使用Storyboard.Completed事件。

另一方面,如果您希望旧图像淡出新图像淡入,则需要更改您的设计以包含占据相同屏幕空间的两个图像更改故事板以使用ParallelTimeline

答案 1 :(得分:0)

能够弄清楚你的意思是里克。我确实让图像淡出,替换并淡入下面的代码....

        private void CoverArt_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Storyboard AnimFadeOut = new Storyboard();
        AnimFadeOut.Completed += new EventHandler(ImageFadeOut_Completed);

        DoubleAnimation FadeOut = new DoubleAnimation();
        FadeOut.From = 1.0;
        FadeOut.To = 0.0;
        FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));

        AnimFadeOut.Children.Add(FadeOut);
        Storyboard.SetTargetName(FadeOut, CurrentCover.Name);
        Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Image.OpacityProperty));


        AnimFadeOut.Begin(this);
    }

    void ImageFadeOut_Completed(object sender, EventArgs e)
    {
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        image.UriSource = new Uri("Minogue.jpg", UriKind.Relative);
        image.EndInit();

        CurrentCover.Source = image;
        Storyboard ImageFadeIn = new Storyboard();

        DoubleAnimation FadeIn = new DoubleAnimation();
        FadeIn.From = 0.0;
        FadeIn.To = 1.0;
        FadeIn.Duration = new Duration(TimeSpan.FromSeconds(.5));

        ImageFadeIn.Children.Add(FadeIn);
        Storyboard.SetTargetName(FadeIn, CurrentCover.Name);
        Storyboard.SetTargetProperty(FadeIn, new PropertyPath(Image.OpacityProperty));
        ImageFadeIn.Begin(this);
    }