How To Create A Gradient Background

H

HenryC

Thanks,
I am new to VB and have already read quite a bit without getting it right.
Some code snippet would be nice.
 
J

JohnFol

Stick this behind the form



Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

MyBase.OnPaint(e)

Dim baseBackground As System.Drawing.Drawing2D.LinearGradientBrush

baseBackground = New LinearGradientBrush(New Point(0, 0), _

New Point(ClientSize.Width, 0), _

Color.Blue, _

Color.Red)

e.Graphics.FillRectangle(baseBackground, ClientRectangle)

End Sub
 
H

HenryC

Thanks John

JohnFol said:
Stick this behind the form



Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

MyBase.OnPaint(e)

Dim baseBackground As System.Drawing.Drawing2D.LinearGradientBrush

baseBackground = New LinearGradientBrush(New Point(0, 0), _

New Point(ClientSize.Width, 0), _

Color.Blue, _

Color.Red)

e.Graphics.FillRectangle(baseBackground, ClientRectangle)

End Sub
 
J

Jay B. Harlow [MVP - Outlook]

Henry,
In addition to the other comments.

I've set the BackgroundImage of the from to an image that was painted with a
gradient brush, something like:

Protected Overrides Sub OnLayout(ByVal e As LayoutEventArgs)
MyBase.OnLayout(e)

If Not Me.BackgroundImage Is Nothing Then
If Size.op_Equality(Me.BackgroundImage.Size, Me.ClientSize) Then
Exit Sub
End If
Me.BackgroundImage.Dispose()
Me.BackgroundImage = Nothing
End If

Dim rect As New Rectangle(New Point(0, 0), Me.ClientSize)
Dim image As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim gr As Graphics = Graphics.FromImage(image)

Dim brush As New LinearGradientBrush(rect,
ControlPaint.LightLight(Me.BackColor), Me.BackColor,
LinearGradientMode.Vertical)
brush.SetBlendTriangularShape(2 / 3)

gr.FillRectangle(brush, rect)
gr.Dispose()
brush.Dispose()

Me.BackgroundImage = image
End Sub

Hope this helps
Jay

| How does one create a gradient background for a form?
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top