A fill tool for bitmaps

Posted on 2004-11-29
Last Modified: 2013-12-03
Hi guys,
I would like to create a fill tool just like the fill tool in Microsoft paint. How would I go about doing it?
Question by:Jas001
    1 Comment
    LVL 85

    Accepted Solution

    Here is one way to do it.  I originally used recursion in the fill routine, but it seemed to cause stack overflows if the region was too big.

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
        Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
        Friend WithEvents Button3 As System.Windows.Forms.Button
        Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button
            Me.PictureBox1 = New System.Windows.Forms.PictureBox
            Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
            Me.Button3 = New System.Windows.Forms.Button
            Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
            Me.Button1.BackColor = System.Drawing.Color.Red
            Me.Button1.Location = New System.Drawing.Point(8, 48)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(64, 32)
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Color"
            Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.PictureBox1.Location = New System.Drawing.Point(80, 8)
            Me.PictureBox1.Name = "PictureBox1"
            Me.PictureBox1.Size = New System.Drawing.Size(200, 248)
            Me.PictureBox1.TabIndex = 1
            Me.PictureBox1.TabStop = False
            Me.Button3.Location = New System.Drawing.Point(8, 8)
            Me.Button3.Name = "Button3"
            Me.Button3.Size = New System.Drawing.Size(64, 32)
            Me.Button3.TabIndex = 3
            Me.Button3.Text = "Image"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private stk As New Stack

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog = DialogResult.OK Then
                Button1.BackColor = ColorDialog1.Color
            End If
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Sub

        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            If Not (PictureBox1.Image Is Nothing) Then
                If e.X < PictureBox1.Image.Width AndAlso e.Y < PictureBox1.Image.Height Then
                    Cursor.Current = Cursors.WaitCursor

                    Dim bmp As New Bitmap(PictureBox1.Image)
                    If Not bmp.GetPixel(e.X, e.Y).Equals(Button1.BackColor) Then
                        fillRegion(bmp, Button1.BackColor, bmp.GetPixel(e.X, e.Y), e.X, e.Y)
                        PictureBox1.Image = bmp
                    End If

                    Cursor.Current = Cursors.Default
                End If
            End If
        End Sub

        Private Sub fillRegion(ByVal bmp As Bitmap, ByVal fillColor As Color, ByVal replaceColor As Color, ByVal x As Integer, ByVal y As Integer)
            Dim p As Point
            stk.Push(New Point(x, y))
            While stk.Count > 0
                p = CType(stk.Pop(), Point)
                x = p.X
                y = p.Y
                If x >= 0 AndAlso x < bmp.Width AndAlso y >= 0 AndAlso y < bmp.Height Then
                    If bmp.GetPixel(x, y).Equals(replaceColor) Then
                        bmp.SetPixel(x, y, fillColor)

                        If y - 1 >= 0 AndAlso bmp.GetPixel(x, y - 1).Equals(replaceColor) Then
                            stk.Push(New Point(x, y - 1))
                        End If

                        If x + 1 < bmp.Width AndAlso bmp.GetPixel(x + 1, y).Equals(replaceColor) Then
                            stk.Push(New Point(x + 1, y))
                        End If

                        If y + 1 < bmp.Height AndAlso bmp.GetPixel(x, y + 1).Equals(replaceColor) Then
                            stk.Push(New Point(x, y + 1))
                        End If

                        If x - 1 >= 0 AndAlso bmp.GetPixel(x - 1, y).Equals(replaceColor) Then
                            stk.Push(New Point(x - 1, y))
                        End If
                    End If
                End If
            End While
        End Sub

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    sql server/ 15 26
    Wordmerge 8 31
    Help file 1 28
    Help with assigning value of previous row to new row added in DataGridView 8 23
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now