图像过渡

发布于 2024-10-12 00:26:01 字数 1399 浏览 4 评论 0原文

我又被卡住了。

我正在用 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);
}

我更愿意在代码后面执行此操作,因为这就是我获取图像的地方。否则,我不确定如何触发它。

一个例子将不胜感激。

I've gotten stuck again.

I'm creating a WPF project in C#...

I've got an image (album cover) that I'll be changing in code behind, and wish to basicaly do the following.

When a new album cover image is determine and aquired, I want the current image in the image control to fade out, get updated with the new cover, and then fade back in.

I'm not seeing very many good examples on how to accomplish this in code behind.

The following was my latest failed attempt...

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

I'd prefer to do this in code behind simply because that is where I'm aquiring the image. Otherwise, I'm not sure how I'd trigger it.

An example would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-10-19 00:26:01

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

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

If you want the old image to fade out and then the new one to fade in using the same image target, then you have to wait for the first storyboard to complete before you change the image and start the new one. To do this use the Storyboard.Completed event.

On the other hand, if you want to the old image to fade out while the new image is fading in, you need to change your design to include two images that occupy the same screen space and change your storyboard to use a ParallelTimeline.

陪我终i 2024-10-19 00:26:01

能够弄清楚你的意思瑞克。我确实使用下面的代码使图像淡出、替换和淡入......

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

was able to figure out what you meant Rick. I do have the image fading out, replaced, and fading in with the code below....

        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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文