Connor Albright
Connor Albright

Reputation: 743

How do I draw a circular gradient?

How do I draw a circular gradient like this in vb.net?

A Red Circular Gradient

Upvotes: 3

Views: 4869

Answers (1)

Paul Sasik
Paul Sasik

Reputation: 81489

Check out this great page. The code in the article is in C#. Here is a VB.NET port of the code you're interested in and updated for a rectangular fill (based on the article's triangle fill sample):

    Dim pgb As New PathGradientBrush(New Point() { _
        New Point(0, 0), _
        New Point(0, Me.ClientRectangle.Height), _
        New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _
        New Point(Me.ClientRectangle.Width, 0)})
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
pgb.Dispose()

Here's another possible solution:

Dim pth As New GraphicsPath()
pth.AddEllipse(Me.ClientRectangle)
Dim pgb As New PathGradientBrush(pth)
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)

Note that this last code snippet will draw a circle bounded inside of a rectangle. If you want the circular gradient to fill the entire rectangle you'll have to calculate a larger elliptic path with a larger rectangle.

Upvotes: 5

Related Questions