Results 1 to 4 of 4

Thread: [2008] how do i (flood-fill) in vb.net ?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Location
    Springfield, Mo.
    Posts
    45

    Question [2008] how do i (flood-fill) in vb.net ?

    hi.

    I am trying to convert a VB-6 program to VB-2008.

    the program displays the (us) map in a picture box.

    during runtime .. the program paints certain states red .. depending on the user's input.

    You can see what the (vb-6) program (and the us map) looks like
    from my website.

    www (dot) wa0h (dot) com
    the (0) in (wa0h) is the number zero.

    web searching revealed that lots of programmers wanted to do this
    but no solutions were found.

    it seems that (gdi+) does (not) have a (ext-flood-fill) function.

    since states do not have a regular boundary, i can't use ..fill-region

    ??? can anyone help me with this simple tasks ???

    searching your forum .. i saw similar postings from several years ago,
    but no apparent solution.

    Thanks for you time.

    Jerry Gentry

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [2008] how do i (flood-fill) in vb.net ?

    try this, its the fastest way of floodfilling a part of a bitmap:

    vb Code:
    1. ' Flood the area at this point.
    2. Public Sub UnsafeFloodFill(ByVal bm As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal new_color As Color)
    3.     ' Get the old and new colors' components.
    4.     Dim old_r As Byte = bm.GetPixel(x, y).R
    5.     Dim old_g As Byte = bm.GetPixel(x, y).G
    6.     Dim old_b As Byte = bm.GetPixel(x, y).B
    7.  
    8.     Dim new_r As Byte = new_color.R
    9.     Dim new_g As Byte = new_color.G
    10.     Dim new_b As Byte = new_color.B
    11.  
    12.     ' Start with the original point in the stack.
    13.     Dim pts As New Stack(1000)
    14.     pts.Push(New Point(x, y))
    15.     bm.SetPixel(x, y, new_color)
    16.  
    17.     ' Make a BitmapBytesARGB32 object.
    18.     Dim bm_bytes As New BitmapBytesARGB32(bm)
    19.  
    20.     ' Lock the bitmap.
    21.     bm_bytes.LockBitmap()
    22.  
    23.     ' While the stack is not empty, process a point.
    24.  
    25.     Do While pts.Count > 0
    26.         Dim pt As Point = DirectCast(pts.Pop(), Point)
    27.         If pt.X > 0 Then UnsafeCheckPoint(bm_bytes, pts, pt.X - 1, pt.Y, old_r, old_g, old_b, new_r, new_g, new_b)
    28.         If pt.Y > 0 Then UnsafeCheckPoint(bm_bytes, pts, pt.X, pt.Y - 1, old_r, old_g, old_b, new_r, new_g, new_b)
    29.         If pt.X < bm.Width - 1 Then UnsafeCheckPoint(bm_bytes, pts, pt.X + 1, pt.Y, old_r, old_g, old_b, new_r, new_g, new_b)
    30.         If pt.Y < bm.Height - 1 Then UnsafeCheckPoint(bm_bytes, pts, pt.X, pt.Y + 1, old_r, old_g, old_b, new_r, new_g, new_b)
    31.     Loop
    32.  
    33.     ' Unlock the bitmap.
    34.     bm_bytes.UnlockBitmap()
    35. End Sub

    vb Code:
    1. Imports System.Drawing.Imaging
    2. Imports System.Runtime.InteropServices
    3.  
    4. Public Class BitmapBytesARGB32
    5.     ' Provide public access to the picture's byte data.
    6.     Public ImageBytes() As Byte
    7.     Public RowSizeBytes As Integer
    8.     Public Const PixelSizeBytes As Integer = 4                  ' 4 bytes/pixel.
    9.     Public Const PixelSizeBits As Integer = PixelSizeBytes * 8  ' 32 bits per pixel.
    10.  
    11.     ' A reference to the Bitmap.
    12.     Private m_Bitmap As Bitmap
    13.  
    14.     ' Save a reference to the bitmap.
    15.     Public Sub New(ByVal bm As Bitmap)
    16.         m_Bitmap = bm
    17.     End Sub
    18.  
    19.     ' Bitmap data.
    20.     Private m_BitmapData As BitmapData
    21.  
    22.     ' Lock the bitmap's data.
    23.     Public Sub LockBitmap()
    24.         ' Lock the bitmap data.
    25.         Dim bounds As Rectangle = New Rectangle( _
    26.             0, 0, m_Bitmap.Width, m_Bitmap.Height)
    27.         m_BitmapData = m_Bitmap.LockBits(bounds, _
    28.             Imaging.ImageLockMode.ReadWrite, _
    29.             Imaging.PixelFormat.Format32bppArgb)
    30.         RowSizeBytes = m_BitmapData.Stride
    31.  
    32.         ' Allocate room for the data.
    33.         Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
    34.         ReDim ImageBytes(total_size)
    35.  
    36.         ' Copy the data into the ImageBytes array.
    37.         Marshal.Copy(m_BitmapData.Scan0, ImageBytes, _
    38.             0, total_size)
    39.     End Sub
    40.  
    41.     ' Copy the data back into the Bitmap
    42.     ' and release resources.
    43.     Public Sub UnlockBitmap()
    44.         ' Copy the data back into the bitmap.
    45.         Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
    46.         Marshal.Copy(ImageBytes, 0, _
    47.             m_BitmapData.Scan0, total_size)
    48.  
    49.         ' Unlock the bitmap.
    50.         m_Bitmap.UnlockBits(m_BitmapData)
    51.  
    52.         ' Release resources.
    53.         ImageBytes = Nothing
    54.         m_BitmapData = Nothing
    55.     End Sub
    56. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [2008] how do i (flood-fill) in vb.net ?

    you need this sub too:

    vb Code:
    1. ' See if this point should be added to the stack.
    2. Private Sub UnsafeCheckPoint(ByVal bm_bytes As BitmapBytesARGB32, ByVal pts As Stack, ByVal x As Integer, ByVal y As Integer, ByVal old_r As Byte, ByVal old_g As Byte, ByVal old_b As Byte, ByVal new_r As Byte, ByVal new_g As Byte, ByVal new_b As Byte)
    3.     Dim pix As Integer = y * bm_bytes.RowSizeBytes + x * 4
    4.     Dim b As Byte = bm_bytes.ImageBytes(pix)
    5.     Dim g As Byte = bm_bytes.ImageBytes(pix + 1)
    6.     Dim r As Byte = bm_bytes.ImageBytes(pix + 2)
    7.  
    8.     If (r = old_r) AndAlso (g = old_g) AndAlso (b = old_b) Then
    9.         pts.Push(New Point(x, y))
    10.         bm_bytes.ImageBytes(pix) = new_b
    11.         bm_bytes.ImageBytes(pix + 1) = new_g
    12.         bm_bytes.ImageBytes(pix + 2) = new_r
    13.     End If
    14. End Sub

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [2008] how do i (flood-fill) in vb.net ?

    believe me jerry, i did a lot of research on this one when i was writing a vb.net paint application. the only alternative is to write your app. in vb6.

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