Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Queue-Linear Flood Fill: A Fast Flood Fill Algorithm

Rate me:
Please Sign up or sign in to vote.
4.92/5 (44 votes)
15 Nov 20066 min read 236.6K   10K   59  
A super-fast flood fill algorithm and implementation, plus helpful optimization tips for image processing.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace PictureBoxScroll
{
    
    public partial class PicturePanel : Panel
    {
        internal bool newMethod = true;//for demo

        public PicturePanel():base()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.Opaque,true);
        }

        private EditableBitmap image;

        public EditableBitmap Image
        {
            get { return image; }
            set { image = value; Refresh();}
        }



        protected override void OnPaintBackground(PaintEventArgs e)
        {
 
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            //these settings aren't even needed for good perf, but they are helpful
            pe.Graphics.InterpolationMode = InterpolationMode.Low;
            pe.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
            pe.Graphics.SmoothingMode = SmoothingMode.HighSpeed;

            if (image != null)
            {
            	//draw empty area, if it exists, in an optimized way.
                if(AutoScrollPosition.X==0)
                {
                    int emptyRightAreaWidth=Width-image.Bitmap.Width;
                    if(emptyRightAreaWidth>0)
                    {
                        Rectangle fillRect=new Rectangle(image.Bitmap.Width,0,emptyRightAreaWidth,Height);
                        fillRect.Intersect(pe.ClipRectangle);
                        pe.Graphics.FillRectangle(SystemBrushes.Control,fillRect);
                    }
                }

                if(AutoScrollPosition.Y==0)
                {
                    int emptyRightAreaHeight=Height-image.Bitmap.Height;
                    if(emptyRightAreaHeight>0)
                    {
                        Rectangle fillRect=new Rectangle(0,image.Bitmap.Height,Width,emptyRightAreaHeight);
                        fillRect.Intersect(pe.ClipRectangle);
                        pe.Graphics.FillRectangle(SystemBrushes.Control,fillRect);
                    }
                }

                //calculate the visible area of the bitmap
                Rectangle bitmapRect = new Rectangle(AutoScrollPosition.X, AutoScrollPosition.Y, image.Bitmap.Width, image.Bitmap.Height);
                Rectangle visibleClientRect = bitmapRect;
                visibleClientRect.Intersect(pe.ClipRectangle);
                if (visibleClientRect.Width == 0 || visibleClientRect.Height == 0)
                    return;
                Rectangle visibleBitmapRect = visibleClientRect;
                visibleBitmapRect.Offset(-AutoScrollPosition.X, -AutoScrollPosition.Y);

                //draw bitmap
                if (newMethod)
                {
                    using(EditableBitmap section = image.CreateView(visibleBitmapRect))
                        pe.Graphics.DrawImageUnscaled(section.Bitmap, visibleClientRect.Location);
                }
                else //normal method
                {
                    pe.Graphics.DrawImage(image.Bitmap, visibleClientRect, visibleBitmapRect, GraphicsUnit.Pixel);
                }
            }
            else //if no bitmap just fill with background color
                pe.Graphics.FillRectangle(SystemBrushes.Control, pe.ClipRectangle);
        }

        public void QuickUpdate(Rectangle rect)
        {
            OnPaint(new PaintEventArgs(CreateGraphics(),rect));
        }

        public void SetPixel(Brush brush, int bmpX, int bmpY)
        {
            using(Graphics g = CreateGraphics())
                g.FillRectangle(brush,new Rectangle(bmpX,bmpY,1,1));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

Comments and Discussions