2

我有这段代码可以让我在我的 windows 窗体中添加一些混合:

public partial class Form1 : Form
{
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, 
               Color.White, 
               Color.Black, 
               LinearGradientMode.Vertical))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    }
}

这是结果:

在此处输入图像描述

默认情况下,两种颜色混合的“峰值”正好在方框的中间。我想调整代码,使混合的“峰值”出现在顶部的大约 3/4 处。是否可以更改两种颜色开始混合的点?

先感谢您。

4

1 回答 1

3

您可以将InterpolationColors画笔的属性设置为合适ColorBlend的,例如:

using (var brush = new LinearGradientBrush(this.ClientRectangle,
    Color.Transparent, Color.Transparent, LinearGradientMode.Vertical))
{
    var blend = new ColorBlend();
    blend.Positions = new[] { 0, 3 / 4f, 1 };
    blend.Colors = new[] { Color.White, Color.Black, Color.Black };
    brush.InterpolationColors = blend;
    e.Graphics.FillRectangle(brush, this.ClientRectangle);
}

在此处输入图像描述

或者例如另一种混合:

blend.Positions = new[] { 0, 1 / 2f, 1 };
blend.Colors = new[] { Color.White, Color.Gray, Color.Black };

在此处输入图像描述

于 2016-11-03T22:06:13.863 回答