Results 1 to 6 of 6

Thread: [2005] graphics question

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Question [2005] graphics question

    i got this floodfill code from the internet, but its too slow.

    Code:
    Public Sub FloodFill(ByVal bm As Bitmap, ByVal x As _
        Integer, ByVal y As Integer, ByVal new_color As Color)
            ' Get the old and new colors.
            Dim old_color As Color = bm.GetPixel(x, y)
    
            ' Start with the original point in the stack.
            Dim pts As New Stack(100)
            pts.Push(New Point(x, y))
            bm.SetPixel(x, y, new_color)
    
            ' While the stack is not empty, process a point.
            Do While pts.Count > 0
                Dim pt As Point = DirectCast(pts.Pop(), Point)
                If pt.X > 0 Then SafeCheckPoint(bm, pts, pt.X - 1, _
                    pt.Y, old_color, new_color)
                If pt.Y > 0 Then SafeCheckPoint(bm, pts, pt.X, pt.Y _
                    - 1, old_color, new_color)
                If pt.X < bm.Width - 1 Then SafeCheckPoint(bm, pts, _
                    pt.X + 1, pt.Y, old_color, new_color)
                If pt.Y < bm.Height - 1 Then SafeCheckPoint(bm, _
                    pts, pt.X, pt.Y + 1, old_color, new_color)
            Loop
        End Sub
    
        Private Sub SafeCheckPoint(ByVal bm As Bitmap, ByVal pts As _
            Stack, ByVal x As Integer, ByVal y As Integer, ByVal _
            old_color As Color, ByVal new_color As Color)
            Dim clr As Color = bm.GetPixel(x, y)
            If clr.Equals(old_color) Then
                pts.Push(New Point(x, y))
                bm.SetPixel(x, y, new_color)
            End If
        End Sub
    does anyone know of a faster way to floodfill an area?

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] graphics question

    I'm not sure what you mean by "floodfill". Can you elaborate on what you are actually trying to do?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] graphics question

    i'm trying to change a region of same colored pixels to a different color

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] graphics question

    Is your region restricted by a certain perimeter or does it apply to an entire bitmap..... or both?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] graphics question

    its a paint program.
    the region could be any shape, part or complete bitmap.

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] graphics question

    Either way, it is going to have to loop through all of the pixels in the region. If you want to speed it up a little, consolidate your code into a single method. You're losing speed by leaving the loop to process another method. Also, avoid creating variables for data that you already have, whenever possible.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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