VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



How to tile an image on a form.

by Vicky (J) (2 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 30th December 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

How to tile an image on a form.

API Declarations



We will begin by creating a module (.bas file). In the General Declarations of our new module we will declare the BitBlt API Call, and a constant value to be used with this function.


#If Win32 Then
Declare Function BitBlt Lib "gdi32" _
(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, _
ByVal ySrc As Long, ByVal dwRop As Long) As Long
#Else
Declare Function BitBlt Lib "gdi" (ByVal hDestDC As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, _
ByVal ySrc As Integer, ByVal dwRop As Long) As Integer
#End If
Const SRCCOPY = &HCC0020
'--end code block
As you can see here I used conditional compilation to use the proper API call for either the 32 bit or 16 bit environment.

Now we will make the TilePicture procedure that will tile the picture passed to it onto the form. Go ahead and enter the code as you see it below.

Rate How to tile an image on a form.



    Dim iPicWidth As Integer
    Dim iPicHeight As Integer
    Dim iFormWidth As Integer
    Dim iFormHeight As Integer
    Dim x As Integer
    Dim y As Integer
    Dim iResult As Integer
    Dim iOldScale As Integer
    
    'Get the picture width and height in pixels
    iOldScale = picSource.ScaleMode
    picSource.ScaleMode = 3
    iPicWidth = picSource.ScaleWidth
    iPicHeight = picSource.ScaleHeight
    picSource.ScaleMode = iOldScale
    
    'Get the Forms width and height in pixels
    iFormWidth = frmDest.Width / Screen.TwipsPerPixelX
    iFormHeight = frmDest.Height / Screen.TwipsPerPixelY
    
    For x = 0 To iFormWidth Step iPicWidth
        For y = 0 To iFormHeight Step iPicHeight
            iResult = BitBlt(frmDest.hDC, x, y, iPicWidth, iPicHeight, _
            picSource.hDC, 0, 0, SRCCOPY)
        Next
    Next
End Sub
'--end code block
As you can see we are passsing two parameters to this function. The destination form and the source picture.

The first thing we do is get the width of the source picture in pixels. In this example I temporarily change the Scalemode to pixels then after I get the width and height I change it back.

Next I get the Form width and height. I did this differently than the Picture just to show a different way to do the same thing.

Finally we loop through the x and y values and BitBlt the picture onto the form.

Now to test this procedure you will need to create a new form and place a picturebox anywhere on the form. Set the picture property to any bitmap. Try a smaller bitmap so you can see that it is tiling properly. 

In the Form_Paint event insert this code.

Private Sub Form_Paint()
    TilePicture Me, Picture1
End Sub
'--end code block
Thats all there is to it. Whenever the form get painted it will tile the image onto the background.

 


Download this snippet    Add to My Saved Code

How to tile an image on a form. Comments

No comments have been posted about How to tile an image on a form.. Why not be the first to post a comment about How to tile an image on a form..

Post your comment

Subject:
Message:
0/1000 characters