Click here to Skip to main content
13,291,084 members (57,900 online)
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads


57 bookmarked
Posted 15 Nov 2006

Queue-Linear Flood Fill: A Fast Flood Fill Algorithm

, 15 Nov 2006
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);

        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.
                    int emptyRightAreaWidth=Width-image.Bitmap.Width;
                        Rectangle fillRect=new Rectangle(image.Bitmap.Width,0,emptyRightAreaWidth,Height);

                    int emptyRightAreaHeight=Height-image.Bitmap.Height;
                        Rectangle fillRect=new Rectangle(0,image.Bitmap.Height,Width,emptyRightAreaHeight);

                //calculate the visible area of the bitmap
                Rectangle bitmapRect = new Rectangle(AutoScrollPosition.X, AutoScrollPosition.Y, image.Bitmap.Width, image.Bitmap.Height);
                Rectangle visibleClientRect = bitmapRect;
                if (visibleClientRect.Width == 0 || visibleClientRect.Height == 0)
                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.


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


About the Author

J. Dunlap
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 and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 15 Nov 2006
Article Copyright 2006 by J. Dunlap
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid