Click here to Skip to main content
13,043,898 members (53,179 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 2 May 2013

Image Region Recoloring

, 2 May 2013
Rate this:
Please Sign up or sign in to vote.
Fill Image region by selected color


Fill with color in photo editors change specific area color with selected color. This job may be needed in other image processing tasks. The current tip shows one simple way to implement fill with color.



  1. Select and load source image
  2. Select position and get its color in image
    Pictures have specific size that is divided in width and height, that each pixel has a certain position X and Y, for easy pixel selection picture size after load time must be equal to the actual size (for scale, avoid any easy pixel selection)
  3. Select new color
  4. Fill Color

    Changing pixels color continue until color has been changed, on the other hand, region borders reach when it finds pixels with different color. The algorithm that is used in this approach is:

    1. Change Color of first selected pixel and add that position to queue
    2. While queue is not empty, do the following:
      • Dequeue one position
      • Find the neighborhood pixels in left, right, top, down direction positions of the current position
      • Check the color of that position pixel if same with the first color:
      • Change pixel color to fill color
      • Add to queue
    3. Save image

Using the Code

I implemented the Fill method:

private void btnFill_Click(object sender, EventArgs e)
    if (SColor == true && isload == true)
        Queue q = new Queue();
        ArrayList PreList = new ArrayList();
        int x1 = int.Parse(txtX.Text);
        int y1 = int.Parse(txtY.Text);
        Point p = new Point(x1, y1);
        img.SetPixel(x1, y1, pic3.BackColor);
        while (q.Count > 0)
            p = (Point)q.Dequeue();
            x1 = p.X;
            y1 = p.Y;
            for (int i = 1; i <= 4; i++)
                int x2 = x1;
                int y2 = y1;
                switch (i)
                    case 1://left
                    case 2://Right
                    case 3://down
                    case 4://up
                if (x2 < img.Width && x2 >= 0 && y2 < img.Height && y2 >= 0)
                    if (img.GetPixel(x2, y2) == pic2.BackColor)
                        img.SetPixel(x2, y2, pic3.BackColor);
                        Point p2 = new Point(x2, y2);
        pic1.Image = img;

Input image (that isn't original photos, because of reducing web page size):

Output image after two fills:

Form view:

Points of Interest

Fill any type of image in color range region.


  • Implemented in April 2013


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mojtaba Eng
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
Software developer

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 2 May 2013
Article Copyright 2013 by Mojtaba Eng
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid