Gradient With A Stroke

I’m trying to fill a rectangle with a gradient and give it a 1 pt stroke. I’m starting out with this:

Var linearBrush As New LinearGradientBrush
linearBrush.StartPoint = New Point(0, 0)
linearBrush.EndPoint = New Point(g.Width, g.Height)
linearBrush.GradientStops.Add(New Pair(0.0, &cCBBFB3))
linearBrush.GradientStops.Add(New Pair(0.4, &cA2968A))

Now, if I add this, I get the gradient:

g.Brush = linearBrush
g.FillRectangle(0, 0, g.Width, g.Height)

And if I add this, I get the stroke, but only if I comment out the code that gives me the gradient:

g.ForeColor = rgb(0, 0, 0)
g.penHeight = 2
g.penWidth = 2
g.DrawRectangle(0, 0, Me.Width, Me.Height)

Does anyone know what I’m doing wrong?

Thanks!

It’s because your stroke is being drawn with the brush that you set up to draw the gradient. The stroke is there, it just looks exactly like the background. :slight_smile: Add g.brush=nil before you draw the stroke and you’ll be fine.

Also - don’t forget to offset your stroking rectangle, to ensure you see the entire width of the stroke:

Var strokeWidth as integer = 2
Var strokeOffset as double

strokeOffset = strokeWidth/2

g.DrawRectangle(strokeOffset, strokeOffset, Me.Width-strokeOffset, Me.Height-strokeOffset)
2 Likes

Worked like a charm! Very much appreciated!