Resizing Background Image

  • Thread starter Thread starter Nathan
  • Start date Start date
* "Nathan said:
How doy you resize the background image to fill a form
when the form is resized?

Add this to the form's constructor:

\\\
SetStyle(ControlStyles.ResizeRedraw, True)
UpdateStyles()
///

Then you can draw the image in the form's 'Paint' event handler.
 
Nathan,
In addition to Herfried's suggestion.

I would consider creating a new Bitmap in the Resize event to the same size
as the Client Rectangle. In the Resize event use the Graphics.Draw* methods
to draw your background onto this Bitmap. I would then simply set the
BackgroundImage property of the form in the Resize event letting the Form do
all the work.

This obviously works best if you have a simply background like a gradient
brush shown. For more a more complex background I would consider staying
with the Paint or PaintBackground events & the method Herfried showed.

Something like:

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)

If Not Me.BackgroundImage Is Nothing Then
Me.BackgroundImage.Dispose()
Me.BackgroundImage = Nothing
End If
Dim image As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim gr As Graphics = Graphics.FromImage(image)
Dim rect As New Rectangle(New Point(0, 0), Me.ClientSize)

Dim brush As New LinearGradientBrush(rect, _
Color.RosyBrown, ControlPaint.LightLight(Color.RosyBrown), _
LinearGradientMode.Vertical)
brush.SetBlendTriangularShape(0.33)
gr.FillRectangle(brush, rect)
gr.Dispose()

Me.BackgroundImage = image
End Sub

The normal PaintBackground event handler will use the BackgroundImage image
to tile on the background, seeing as this image is the size of the client
area there is no stretching or tiling involved.

Hope this helps
Jay
 
Back
Top