Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Gradient - apply to a picturebox, 2 colors, 4 directions


Sub ColorPicture(P As PictureBox, Direction As Long , R1 As Integer , G1 As Integer , B1 As Integer , R2 As Integer , G2 As Integer , B2 As Integer )

   P.DrawStyle = vbInsideSolid
   P.DrawMode = vbCopyPen
   P.ScaleMode = vbPixels
   P.AutoRedraw = True
   
   
    Dim i As Integer , iHeight As Integer , iWidth As Integer
    Dim R As Integer , G As Integer , B As Integer
   iWidth = P.ScaleWidth
   iHeight = P.ScaleHeight
       
    Select Case Direction
        Case 1               'bottom to top
            For i = 0 To iHeight
               R = R1 - (R1 - R2) / iHeight * i
               G = G1 - (G1 - G2) / iHeight * i
               B = B1 - (B1 - B2) / iHeight * i
               P.Line (0, i) - (iWidth, i), RGB(R, G, B), B
            Next i
        Case 2               'top to bottom
            For i = 0 To iHeight
               R = R1 - (R1 - R2) / iHeight * i
               G = G1 - (G1 - G2) / iHeight * i
               B = B1 - (B1 - B2) / iHeight * i
               P.Line (0, iHeight - i) - (iWidth, iHeight - i), RGB(R, G, B), B
            Next i
        Case 3               'left to right
            For i = 0 To iWidth
               R = R1 - (R1 - R2) / iHeight * i
               G = G1 - (G1 - G2) / iHeight * i
               B = B1 - (B1 - B2) / iHeight * i
               P.Line (i, 0) - (i, iHeight), RGB(R, G, B), B
            Next i
        Case 4               'right to left
            For i = 0 To iWidth
               R = R1 - (R1 - R2) / iHeight * i
               G = G1 - (G1 - G2) / iHeight * i
               B = B1 - (B1 - B2) / iHeight * i
               P.Line (iWidth - i, 0) - (iWidth - i, iHeight), RGB(R, G, B), B
            Next i
   End Select
   
End Sub