|
-
Jul 2nd, 2007, 10:47 AM
#1
[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?
-
Jul 2nd, 2007, 11:55 AM
#2
Re: [2005] graphics question
I'm not sure what you mean by "floodfill". Can you elaborate on what you are actually trying to do?
-
Jul 2nd, 2007, 12:14 PM
#3
Re: [2005] graphics question
i'm trying to change a region of same colored pixels to a different color
-
Jul 2nd, 2007, 12:20 PM
#4
Re: [2005] graphics question
Is your region restricted by a certain perimeter or does it apply to an entire bitmap..... or both?
-
Jul 2nd, 2007, 12:23 PM
#5
Re: [2005] graphics question
its a paint program.
the region could be any shape, part or complete bitmap.
-
Jul 2nd, 2007, 12:31 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|