【问题标题】:Animate Gradient colors on the line in C# .net在 C# .net 中为线条上的渐变颜色设置动画
【发布时间】:2017-06-13 09:41:19
【问题描述】:

想象一下,我有一条线,它用三种颜色渐变色:深红色、红色和浅红色。我想改变这些颜色在该行中的位置。我的目的是显示某些东西正在沿线移动。 我不知道如何创建动画来改变颜色渐变线中每种颜色的位置。

我发现了这个:https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-animate-the-position-or-color-of-a-gradient-stop

但不是太清楚。

【问题讨论】:

  • 在 winforms 中,您可以使用 LinearGradientBrush 并在 Tick 事件中将起点向外移动..
  • 我添加了它。 @TaW

标签: c# .net winforms animation gradient


【解决方案1】:

这是一个例子:

它使用LineraGradientBrush,将定义矩形的起点移动到左上角,然后在PictureBox上绘制一个旋转矩形:

Point p1 = Point.Empty;

private void timer1_Tick(object sender, EventArgs e)
{
    int deltaX = -3;
    int deltaY = -3;
    p1 = new Point(p1.X + deltaX , p1.Y + deltaY); // roll..
    if (p1.X < deltaX * 1000) p1 = Point.Empty;    // ..around
    pictureBox1.Invalidate();

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    float angle = 33f;
    if (!timer1.Enabled) return;
    Rectangle rectG = new Rectangle(p1.X, p1.Y, 122, 22);
    Rectangle rectR = new Rectangle(22, 22, 222, 22);
    LinearGradientBrush lBrush = new LinearGradientBrush(rectG, 
                                     Color.Red, Color.Red, angle, false);

    ColorBlend cblend = new ColorBlend(5);
    cblend.Colors = new Color[5]  
         { Color.Red, Color.Pink, Color.MistyRose, Color.LightCoral, Color.White };
    cblend.Positions = new float[5] { 0f, 0.2f, 0.5f, 0.8f, 1f };
    lBrush.InterpolationColors = cblend;
    lBrush.WrapMode = WrapMode.TileFlipXY;

    e.Graphics.RotateTransform(angle);
    e.Graphics.TranslateTransform(22,11);
    e.Graphics.FillRectangle(lBrush, rectR);
}

请注意,这是 Winforms,您无法获得真正流畅的动画,但如果您绘制的控件/表单是 DoubleBufered,至少它不会闪烁..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2015-07-13
    • 2015-02-15
    • 1970-01-01
    相关资源
    最近更新 更多