Click here to Skip to main content
12,633,519 members (31,632 online)
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads

Stats

151.5K views
7.1K downloads
55 bookmarked
Posted

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);
            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

Share

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

You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 15 Nov 2006
Article Copyright 2006 by J. Dunlap
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid