[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

A fill tool for bitmaps

Posted on 2004-11-29
1
Medium Priority
?
270 Views
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?
0
Comment
Question by:Jas001
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 12699005
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        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"
        '
        'PictureBox1
        '
        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
        '
        'Button3
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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
            Try
                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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

831 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