Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleFit a PictureBox to its non-transparent pixels
DescriptionThis example shows how to fit a PictureBox to its non-transparent pixels in Visual Basic 6. It makes a region that includes the picture's non-transparent pixels and then restricts the PictureBox to that area with the SetWindowRgn API function.
Keywordsshaped picture, PictureBox, region, SetWindowRgn
CategoriesGraphics, API
 
The program uses the GetDIBits API function to examine the picture's pixels row-by-row. For each block of non-transparent pixels, it makes a rectangular region. It combines the regions and uses SetWindowRgn to restrict the PictureBox to the combined region.
 
' Restrict the form to its "transparent" pixels.
Private Sub TrimPicture(ByVal pic As PictureBox, ByVal _
    transparent_color As Long)
Const RGN_OR = 2
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim transparent_r As Byte
Dim transparent_g As Byte
Dim transparent_b As Byte
Dim wid As Integer
Dim hgt As Integer
Dim X As Integer
Dim Y As Integer
Dim start_x As Integer
Dim stop_x As Integer
Dim combined_rgn As Long
Dim new_rgn As Long

    ' Prepare the bitmap description.
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = picShape.ScaleWidth
        ' Use negative height to scan top-down.
        .biHeight = -picShape.ScaleHeight
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
            31) \ 32) * 4)
        pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
            * .biBitCount) + 7) \ 8)
        .biSizeImage = bytes_per_scanLine * Abs(.biHeight)
    End With

    ' Load the bitmap's data.
    wid = picShape.ScaleWidth
    hgt = picShape.ScaleHeight
    ReDim pixels(1 To 4, 0 To wid - 1, 0 To hgt - 1)
    GetDIBits picShape.hdc, picShape.Image, _
        0, picShape.ScaleHeight, pixels(1, 0, 0), _
        bitmap_info, DIB_RGB_COLORS

    ' Break the transparent color into its components.
    UnRGB transparent_color, transparent_r, transparent_g, _
        transparent_b

    ' Create the PictureBox's regions.
    For Y = 0 To hgt - 1
        ' Create a region for this row.
        X = 1
        Do While X < wid
            start_x = 0
            stop_x = 0

            ' Find the next non-transparent column.
            Do While X < wid
                If pixels(pixR, X, Y) <> transparent_r Or _
                   pixels(pixG, X, Y) <> transparent_g Or _
                   pixels(pixB, X, Y) <> transparent_b _
                Then
                    Exit Do
                End If
                X = X + 1
            Loop
            start_x = X

            ' Find the next transparent column.
            Do While X < wid
                If pixels(pixR, X, Y) = transparent_r And _
                   pixels(pixG, X, Y) = transparent_g And _
                   pixels(pixB, X, Y) = transparent_b _
                Then
                    Exit Do
                End If
                X = X + 1
            Loop
            stop_x = X

            ' Make a region from start_x to stop_x.
            If start_x < wid Then
                If stop_x >= wid Then stop_x = wid - 1

                ' Create the region.
                new_rgn = CreateRectRgn( _
                    start_x, Y, stop_x, Y + 1)

                ' Add it to what we have so far.
                If combined_rgn = 0 Then
                    combined_rgn = new_rgn
                Else
                    CombineRgn combined_rgn, _
                        combined_rgn, new_rgn, RGN_OR
                    DeleteObject new_rgn
                End If
            End If
        Loop
    Next Y
    
    ' Restrict the PictureBox to the region.
    SetWindowRgn pic.hWnd, combined_rgn, True
    DeleteObject combined_rgn
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming, Second Edition.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated