Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version

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.
largepic.zip
queuelinearfloodfill_demo.zip
queuelinearfloodfill_src.zip
FloodFill2
bin
Release
hills_sm.jpg
peaches_sm.jpg
poppy_sm.jpg
Properties
Settings.settings
Resources
NoAction.bmp
OpenFolder.bmp
Save.bmp
snail.png
Thumbs.db
FloodFill2.exe
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

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.

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