Circular gradients in .NET WinForms application

In my WinForms application (C #), I have a circle (defined Rectangle) that I now fill with solid color. I would like to fill this with a circular (not linear) gradient (so that one color in the center gradually fades to another color at the edges).

I experimented with PathGradientBrushbut no luck (I still see a solid color). If anyone has some sample code to do this, that will be awesome!

+3
source share
1 answer

I found a solution here .

private void label1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(label1.ClientRectangle);

    PathGradientBrush pgb = new PathGradientBrush(gp);

    pgb.CenterPoint = new PointF(label1.ClientRectangle.Width / 2, 
                                 label1.ClientRectangle.Height / 2);
    pgb.CenterColor = Color.White;
    pgb.SurroundingColors = new Color[] { Color.Red };

    e.Graphics.FillPath(pgb, gp);

    pgb.Dispose();
    gp.Dispose();
}
+3
source

Source: https://habr.com/ru/post/1775026/


All Articles