【发布时间】:2018-09-22 18:57:08
【问题描述】:
我希望能够制作一个四色渐变,每个角都有一种颜色。我希望能够使用 Windows 窗体 C# 中的图形在矩形绘图中执行此操作。如果可能的话,任何人都可以帮助制作一个代码吗?谢谢。
【问题讨论】:
我希望能够制作一个四色渐变,每个角都有一种颜色。我希望能够使用 Windows 窗体 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);
}
}
结果:
【讨论】: