Results 1 to 4 of 4

Thread: using a flood fill inside a PictureBox

  1. #1

    Thread Starter
    Member Swishy's Avatar
    Join Date
    Feb 2003
    Location
    Huntsville, AL
    Posts
    39

    Talking using a flood fill inside a PictureBox

    Let's presume that I have a PictureBox on my form called "PictureBox1". I have done some line drawing on it (see attached image--LEFT SIDE).

    I want to, given an (x,y) pixel pair, flood file the "white" region to "green" (see attached image--RIGHT SIDE).

    My question is this:

    How can you, within a PictureBox, execute a "flood fill" on a region given the "before" color, the "after" color, and an (x,y) coordinate pair?

    Is this something that can be easily done?

    What I'm ultimately getting at is odd shapes that I will want to fille with different colors.

    Does anyone have any ideas?

  2. #2

    Thread Starter
    Member Swishy's Avatar
    Join Date
    Feb 2003
    Location
    Huntsville, AL
    Posts
    39
    Forgot that file. Heh!
    Attached Images Attached Images  

  3. #3
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    You can do it using the ExtFloodFill API with the FLOODFILLSURFACE flag.

    here's some demo code:
    VB Code:
    1. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    2. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    3. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    4. Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    5.  
    6. Private Declare Function ExtFloodFill Lib "gdi32" _
    7.     (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long, ByVal wFillType As Long) As Long
    8.  
    9. 'Description:
    10. '   The ExtFloodFill function fill an area using the color of the current selected brush in the target device context.
    11. '
    12. 'Params:
    13. '   hdc: Handle the device context in wich the function will be used.
    14. '
    15. '   X: The x-position where the filling will start.
    16. '
    17. '   Y: The y-position where the filling will start.
    18. '
    19. '   crColor: The color value of the boundary or the area to be filled, depending of the wFillType flag.
    20. '
    21. '   wFillType: The type of the fill operation to perform.  Can be:
    22.  
    23.                 Private Const FLOODFILLBORDER = 0
    24.                     'The area to be filled is bounded with crColor, the filling will be done in all directions _
    25.                     'Until crColor is reached.
    26.                    
    27.                 Private Const FLOODFILLSURFACE = 1
    28.                     'The area to be filled is defined by crColor, the filling will be done in all directions _
    29.                     'as long as crColor is encountered.
    30.  
    31. Private Sub Form_Load()
    32.  
    33. Dim BackColor As Long
    34. Dim hPrevBrush As Long
    35. Dim hNewBrush As Long
    36.  
    37.     '--> this just draw a rectangle to the screen to have a shape to fill.
    38.    
    39.     'The long color for the new brush
    40.     BackColor = RGB(13, 168, 252)
    41.     'Create a brush
    42.     hNewBrush = CreateSolidBrush(BackColor)
    43.     'Select the new brush in the dc.
    44.     hPrevBrush = SelectObject(Me.hdc, hNewBrush)
    45.     'Draw a rectangle
    46.     Rectangle Me.hdc, 10, 10, 300, 150
    47.     'Put back the previous brush in the dc.
    48.     SelectObject Me.hdc, hPrevBrush
    49.     'Delete the new brush
    50.     DeleteObject hNewBrush
    51.    
    52. End Sub
    53.  
    54. Public Function ColorFill(hdc As Long, x As Long, y As Long, ColorToFill As Long, FillWithColor As Long)
    55.  
    56. '--> this function will fill an area of ColorToFill with FillWithColor
    57.  
    58. Dim hTempBrush As Long
    59. Dim hPrevBrush As Long
    60.  
    61.     'Create the brush that will be used to fill the area.
    62.     hTempBrush = CreateSolidBrush(FillWithColor)
    63.     'Select the brush into the dc.
    64.     hPrevBrush = SelectObject(hdc, hTempBrush)
    65.     'Fill the area.
    66.     ExtFloodFill hdc, x, y, ColorToFill, FLOODFILLSURFACE
    67.     'Set back the previous brush
    68.     SelectObject hdc, hPrevBrush
    69.     'Delete the temp brush.
    70.     DeleteObject hTempBrush
    71.    
    72. End Function
    73.  
    74. Private Sub Form_Click()
    75.  
    76.     ColorFill Me.hdc, 20, 20, RGB(13, 168, 252), RGB(123, 234, 176)
    77.     Form1.Refresh
    78.    
    79. End Sub

    hope this helps!

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  4. #4
    Lively Member
    Join Date
    Mar 2010
    Posts
    124

    Re: using a flood fill inside a PictureBox

    Oh, very cool, thanks. I have the following code:
    PHP Code:
    Private Declare Function ExtFloodFill Lib "GDI32" (ByVal hDC As Long_
        ByVal X 
    As LongByVal Y As LongByVal colorCode As Long_
        ByVal fillType 
    As Long) As Long
    Const FLOODFILLBORDER 0
    Const FLOODFILLSURFACE 1

    ' Fill a region using the current color or brush
    '   
    OBJ can be a form or a control that exposes
    '       a device context (eg a picture box)
    '   
    X,Y are given in the current system coordinates
    '
    If BORDERCOLOR is specifiedfills the area
    '   enclosed by a border of that color
    If BORDERCOLOR is omittedthe area is filled with
    '   the color now at coordinates (x,y), and any
    '   
    different color is considered to a the border

    Sub AreaFill
    (obj As ObjectByVal X As LongByVal Y As Long_
        ByVal colorCode 
    As LongOptional borderColor As Variant)
        
        
    Dim x2 As Longy2 As Long
        Dim saveFillStyle 
    As Long
        Dim saveFillColor 
    As Long
        
        With obj
            
    ' convert into pixel coordinates
            x2 = .ScaleX(X, .ScaleMode, vbPixels)
            y2 = .ScaleY(Y, .ScaleMode, vbPixels)
            
            ' 
    save FillStyle and FillColor properties
            saveFillStyle 
    = .FillStyle
            saveFillColor 
    = .FillColor
            
    ' enforce new properties
            .FillStyle = 0
            .FillColor = colorCode
            
            If IsMissing(borderColor) Then
                ' 
    get color at given coordinates
                borderColor 
    = .Point(XY)
                
    ' change all the pixels with that color
                ExtFloodFill .hDC, x2, y2, borderColor, FLOODFILLSURFACE
            Else
                ExtFloodFill .hDC, x2, y2, borderColor, FLOODFILLBORDER
            End If

            ' 
    restore properties
            
    .FillStyle saveFillStyle
            
    .FillColor saveFillColor
        End With

    End Sub

    Private Sub Form_Load()
    AreaFill Picture12020RGB(13168252), RGB(123234176)
    Picture1.Refresh
    End Sub 
    You can use it.

    p/s: Source DevX.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width