Let's build a painting 'Fill' routine - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Let's build a painting 'Fill' routine

CitizenOlekCitizenOlek Posts: 53Member
Hi everyone,

As I've been delving into VB.Net I've found a Black.Hole!
An empty space where there should be a 'Fill' routine.
I found some codes on the Net that were way to complex and difficult to insert into an existing project.

Let's build one together!

Comments

  • CitizenOlekCitizenOlek Posts: 53Member
    I was sick of looking for something on the Internet, so I wrote a paint Fill routine myself. It works,but not 100%! Someone needs to check this out and see where I went wrong, also it could use some error checking code.

    See new code below.
  • CitizenOlekCitizenOlek Posts: 53Member
    Hi everyone,

    The response to this thread has left me breathless!
    After several days of intense research and programming I've written a working FloodFill routine! It was more complicated than I'd hoped, having to use the Lock/Unlock Bitmap technique. My routine is v.e.r.y s.l.o.w ( I did my best ) and I'm afraid on a complex image it might run out of room for it's queue. For whatever it's worth, some of the code might be of use to someone, so here it is.
    This 1st part is the Loop that calls the Line painting routine as many times as necessary to complete the Fill.
    [code]
    Do While Qp > 0
    pix = theQ(Qp)
    pY = CInt(Fix(pix / (BMP.Width * 3)))
    NewLinearFill(bm_bytes)
    Loop
    [/code]
    And here is the Main routine
    [code]
    Private Sub NewLinearFill(ByVal bm_bytes As BitmapBytesRGB24)
    savepix = pix
    savepix2 = pix

    'Paint 1st pixel
    bm_bytes.ImageBytes(pix) = R
    bm_bytes.ImageBytes(pix + 1) = G
    bm_bytes.ImageBytes(pix + 2) = B

    '1st pixel done so remove it from the queue
    Qp -= 1
    'This boolean array is used with a tolerance type Fill
    'Every painted pixel must be entered as True
    'Bool((pY * BMP.Width) + pX) = True

    upflag = False
    dnflag = False
    Do 'Loop for painting right

    If pY > 0 Then 'Safe to look up
    pix -= BMP.Width * 3
    If (bm_bytes.ImageBytes(pix) = oldR) And _
    (bm_bytes.ImageBytes(pix + 1) = oldG) And _
    (bm_bytes.ImageBytes(pix + 2) = oldB) Then
    If Not upflag Then
    'Add to queue
    Qp += 1
    theQ(Qp) = pix
    upflag = True
    End If
    Else : upflag = False
    End If
    pix = savepix2
    End If
    If pY < BMP.Height - 1 Then 'Safe to look down
    pix += BMP.Width * 3
    If (bm_bytes.ImageBytes(pix) = oldR) And _
    (bm_bytes.ImageBytes(pix + 1) = oldG) And _
    (bm_bytes.ImageBytes(pix + 2) = oldB) Then
    If Not dnflag Then
    'Add to queue
    Qp += 1
    theQ(Qp) = pix
    dnflag = True
    End If
    Else : dnflag = False
    End If
    pix = savepix2
    End If

    'Move to the right
    '1st check if touching far right edge
    If pix > (pY * (BMP.Width * 3)) + ((BMP.Width * 3) - 6) Then
    'Edge so vamanosse
    Exit Do
    End If

    'Check right
    If (bm_bytes.ImageBytes(pix + 3) <> oldR) And _
    (bm_bytes.ImageBytes(pix + 4) <> oldG) And _
    (bm_bytes.ImageBytes(pix + 5) <> oldB) Then
    'Not Valid so Exit!
    Exit Do
    End If
    'Increment and Paint
    pix += 3
    savepix2 = pix
    bm_bytes.ImageBytes(pix) = R
    bm_bytes.ImageBytes(pix + 1) = G
    bm_bytes.ImageBytes(pix + 2) = B
    Loop

    'Back to where we first came in
    pix = savepix
    savepix2 = pix

    upflag = False
    dnflag = False
    Do 'Loop for painting left
    If pY > 0 Then 'Safe to look up
    pix -= BMP.Width * 3
    If (bm_bytes.ImageBytes(pix) = oldR) And _
    (bm_bytes.ImageBytes(pix + 1) = oldG) And _
    (bm_bytes.ImageBytes(pix + 2) = oldB) Then
    'because we're painting to the left
    'we have to swap the queue value for
    'this scan line for the pix to the left
    If theQ(Qp) = pix + 3 Then
    theQ(Qp) = pix
    Else
    Qp += 1
    theQ(Qp) = pix
    End If
    End If
    pix = savepix2
    End If
    If pY < BMP.Height - 1 Then 'Safe to look down
    pix += BMP.Width * 3
    If (bm_bytes.ImageBytes(pix) = oldR) And _
    (bm_bytes.ImageBytes(pix + 1) = oldG) And _
    (bm_bytes.ImageBytes(pix + 2) = oldB) Then
    'because we're painting to the left
    'we have to swap the queue value for
    'this scan line for the pix to the left
    If theQ(Qp) = pix + 3 Then
    theQ(Qp) = pix
    Else
    Qp += 1
    theQ(Qp) = pix
    End If
    End If
    pix = savepix2
    End If

    '1st check if touching far left edge
    If pix = (pY * (BMP.Width * 3)) Then
    'Edge so vamanosse
    Exit Do
    End If

    'Check left
    If (bm_bytes.ImageBytes(pix - 3) <> oldR) And _
    (bm_bytes.ImageBytes(pix - 2) <> oldG) And _
    (bm_bytes.ImageBytes(pix - 1) <> oldB) Then
    'Not Valid so Exit!
    Exit Do
    End If
    'Decrement and Paint
    pix -= 3
    savepix2 = pix
    bm_bytes.ImageBytes(pix) = R
    bm_bytes.ImageBytes(pix + 1) = G
    bm_bytes.ImageBytes(pix + 2) = B
    Loop

    End Sub
    [/code]
  • clanguageclanguage Posts: 10Member
    Hi there,
    what is vb6? :D

    [code]If (check(mast, 1) < 1) Or (check(mast, 1) > _
    VB6.PixelsToTwipsY(PBox1.Height) - 1) Or (check(mast, 2) < 1) Or _
    (check(mast, 2) > VB6.PixelsToTwipsY(PBox1.Height) - 1) Then Exit Sub[/code]
Sign In or Register to comment.