Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27374

Need help - not all pixels are filled in with code

$
0
0
Hello,

I found the code below for filling in a color up to the border. It works but not all pixels are filled in near the border. Anyone have an idea why? Below is the code and a picture of the image near the border.

Thanks,

Warren

Code:

Public Class Form1
    ' Create a Bitmap object from an image file.
    Dim myBitmap As New Bitmap("C:\Projects\Color1\Color1\c1.jpg")
    Dim pixelColor As Color

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Get the color of a pixel within myBitmap.
        pixelColor = myBitmap.GetPixel(200, 200)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'RecursiveFill(200, 200, Color.Red, Color.Black)
        Dim point1 As New Point(200, 200)
        Flood(point1, myBitmap, Color.Red)
    End Sub

    Private Sub Flood(ByVal startval As Point, ByVal bmp As Bitmap, ByVal Fill As Color)
        Dim s As New Stack
        Dim j As Integer
        s.Push(startval)
        Dim test As Color = bmp.GetPixel(startval.X, startval.Y)
        Dim temp As New Point
        j = 1
        While s.Count <> 0
            temp = s.Pop()
            If bmp.GetPixel(temp.X, temp.Y) = test Then
                bmp.SetPixel(temp.X, temp.Y, Fill)
                If temp.X > 0 Then
                    If bmp.GetPixel(temp.X - 1, temp.Y) = test Then s.Push(New Point(temp.X - 1, temp.Y))
                End If
                If temp.Y > 0 Then
                    If bmp.GetPixel(temp.X, temp.Y - 1) = test Then s.Push(New Point(temp.X, temp.Y - 1))
                End If
                If temp.X < bmp.Width - 1 Then
                    If bmp.GetPixel(temp.X + 1, temp.Y) = test Then s.Push(New Point(temp.X + 1, temp.Y))
                End If
                If temp.Y < -1 + bmp.Height Then
                    If bmp.GetPixel(temp.X, temp.Y + 1) = test Then s.Push(New Point(temp.X, temp.Y + 1))
                End If
            End If
        End While
        myBitmap.Save("C:\Projects\Color1\Color1\c2.jpg")
        MsgBox("Done")
    End Sub
End Class

Attached Images
 

Viewing all articles
Browse latest Browse all 27374


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>