【问题标题】:Four Color Gradient Rectangle in Windows Form C# Using?Windows Form C#中使用四色渐变矩形?
【发布时间】:2018-09-22 18:57:08
【问题描述】:

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

【问题讨论】:

    标签: c# windows forms gradient


    【解决方案1】:

    您可以使用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);
        }
    }
    

    结果:

    【讨论】:

    • 看起来不错。它确实有效,但有没有办法让没有白人中心?
    • @Roee Palmon,我已经修改了删除白色中心并改用平均颜色的尝试。
    • 谢谢!太完美了!
    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多