Visual Basic Window/Form Routines
Pure VB: Tile an Image as a Form Background
     
Posted:   February  27, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-16, VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

FillRect: Gradient Form Backgrounds
GradientFill: Gradient Form Backgrounds
GradientFill: Triangular Gradient Form Backgrounds
Pure VB: Tile an Image as a Form Background
     
 Prerequisites
Suitable Image for the background.

Tiling a bitmap, metafile (and with VB5, a gif or jpeg) is easy using the PaintPicture method.  The code below shows two routines you can place into a form's Paint event to either tile an image across the entire form, or to create a image running along the edge.

Both methods need only one hidden image control each (preloaded in this example with the images to tile). A picture box could also be used for the image(s) to tile, but the added resources used by a picture box aren't warranted in this example.

Start a new project, and on the form place either an Image or Picture control. Set the control's Picture property to the bitmap or metafile (gif or jpeg as well in VB5/6), that you want to use as the form's background image.  Set the control Visible to False.

 

 BAS Module Code
None.

 Form Code
To the form, add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Paint()

    Dim X As Integer, Y As Integer
    Dim ImgWidth As Integer
    Dim ImgHeight As Integer
    Dim FrmWidth As Integer
    Dim FrmHeight As Integer

   'use Image1 or Picture1, as appropriate.
   'use one of the following PaintPicture methods:
    ImgWidth = Image1.Width
    ImgHeight = Image1.Height
    FrmWidth = Form1.Width
    FrmHeight = Form1.Height
    
   'tile entire form (Method 1)
    For X = 0 To FrmWidth Step ImgWidth
       For Y = 0 To FrmHeight Step ImgHeight
          PaintPicture Image1, X, Y      
       Next Y
    Next X

   'tile left side (Method 2)
   'For Y = 0 To FrmHeight Step ImgHeight
   '   PaintPicture Image1, 0, Y 
   'Next Y
    
End Sub
 Comments
Run the project ...the image will paint across the form dependant on the method used. Web background image galleries provide a great place to grab interesting background images.

For VB4, JPEG and GIF backgrounds must be converted to bitmaps or metafiles by a third-party image manipulation program before you can use them.

The PaintPicture method is not available in VB3.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter