VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Let's build a painting 'Fill' routine Posted by CitizenOlek on 9 Jul 2007 at 8:17 AM
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!
Report
Re: Let's build a painting 'Fill' routine Posted by CitizenOlek on 9 Jul 2007 at 8:04 PM
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.
Report
Re: Let's build a painting 'Fill' routine Posted by CitizenOlek on 13 Jul 2007 at 12:19 PM
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.
Do While Qp > 0
    pix = theQ(Qp)
    pY = CInt(Fix(pix / (BMP.Width * 3)))
    NewLinearFill(bm_bytes)
Loop

And here is the Main routine
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

Report
Re: Let's build a painting 'Fill' routine Posted by clanguage on 13 Jul 2007 at 1:50 PM
Hi there,
what is vb6? :D

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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.