Windows 中的四个颜色渐变矩形 C# 使用?

Four Color Gradient Rectangle in Windows Form C# Using?

我希望能够从每个角制作一种颜色的四色渐变。我希望能够使用 Windows Form C# 中的图形在矩形绘图中执行此操作。如果可能的话,任何人都可以帮助制作代码吗?谢谢。

您可以使用 PathGradientBrush 来做到这一点。为了获得平滑的混合效果,我将中心颜色设置为所有相关颜色的平均值。

private void Form1_Paint(object sender, PaintEventArgs e)
{
    var colorArray = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Yellow };        
    GraphicsPath graphicsPath = new GraphicsPath();
    graphicsPath.AddRectangle(ClientRectangle);

    using (Graphics graphics = this.CreateGraphics())
    using (PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath)
    {
        CenterColor = Color.FromArgb((int)colorArray.Average(a => a.R), (int)colorArray.Average(a => a.G), (int)colorArray.Average(a => a.B)),
        SurroundColors = colorArray
    })
    {
        graphics.FillPath(pathGradientBrush, graphicsPath);
    }
}

结果: