简体   繁体   English

如何将 ImageBrush 转换为 Color 以便为 Canvas.Background (WPF) 设置动画

[英]How to convert ImageBrush to Color in order to animate Canvas.Background (WPF)

I'm trying to make an animation for the background of my canvas, passing progressively from an already defined background using an ImageBrush (it's an image representing a battle scene) to other ImageBrush that represents the end of the game screen.我正在尝试为画布的背景制作动画,使用 ImageBrush(它是代表战斗场景的图像)从已经定义的背景逐渐传递到代表游戏屏幕结束的其他 ImageBrush。 Both images fill all the canvas background.两个图像都填充了所有画布背景。 Is there a converter that allows casting ImageBrush to Color?是否有允许将 ImageBrush 转换为 Color 的转换器? Or some kind of BrushAnimation?还是某种 BrushAnimation?

I have tried this but I get System.InvalidCastException: 'No se puede convertir un objeto de tipo 'System.Windows.Media.ImageBrush' al tipo 'System.Windows.Media.SolidColorBrush'.'我试过这个,但我得到 System.InvalidCastException: 'No se puede convertir un objeto de tipo 'System.Windows.Media.ImageBrush' al tipo 'System.Windows.Media.SolidColorBrush'。

            Brush newColor = miLienzo.Background;
            SolidColorBrush newBrush = (SolidColorBrush)newColor;
            Color myColorFromBrush = newBrush.Color;

            endBrush.ImageSource = new BitmapImage(new Uri("Imagenes/fin.jpg", UriKind.Relative));
            Brush newColor2 = endBrush;
            SolidColorBrush newBrush2 = (SolidColorBrush)newColor2;
            Color myColorFromBrush2 = newBrush2.Color;

            ColorAnimation animation;
            animation = new ColorAnimation();
            animation.From = myColorFromBrush;
            animation.To = myColorFromBrush2;
            animation.Duration = new Duration(TimeSpan.FromSeconds(2));
            miLienzo.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);

You can't do what you're trying to do.你不能做你想做的事。

Put an image in the canvas and make the parts you intend animating transparent.将图像放入画布中,并使您想要制作动画的部分变得透明。

Or或者

Trace the static part into a geometry using inkscape and use that to define a shape you similarly place in your canvas.使用inkscape 将静态部分跟踪到几何图形中,并使用它来定义您在画布中类似地放置的形状。

Either way the static parts are separate and on top of the background.无论哪种方式,静态部分都是独立的并且位于背景之上。

Make the background a solidcolorbrush.使背景成为solidcolorbrush。

You can then animate the color in that solidcolorbrush.然后,您可以在该solidcolorbrush 中为颜色设置动画。 That'll work.那行得通。

One advantage of the shape based approach is that this can also have another solidcolorbrush for fill ( and stroke for that matter ).基于形状的方法的一个优点是它也可以有另一个用于填充的纯色画笔(和笔画)。 You could then animate the color of that as well.然后,您也可以为它的颜色设置动画。

Geometries are also fairly lightweight and shapes are vectors so they scale well.几何图形也相当轻量级,形状是矢量,因此它们可以很好地缩放。

Or或者

Overlay two images with your two pictures as source.用您的两张图片作为源叠加两张图片。 Animate the opacity of one down fromk 1.0 to 0 and the other up from 0 to 1.0 to fade one out and the other in. Probably not exactly what you have in mind but could look cool.动画的不透明度从 1.0 到 0 和另一个从 0 到 1.0 淡出,另一个淡入。可能不完全是你的想法,但可能看起来很酷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM