Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Flood Fill in C#

Flood Fill in C#

Scheduled Pinned Locked Moved C#
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    k_crysa
    wrote on last edited by
    #1

    I have just started working in c# so I'm not that familiar with it. Still, I need to build a paint program and I am a bit stuck at Flood Fill. I want to be able to fill areas with an undetermined shape, so I used the SetPixel command, but it sets only one pixel (I'm not entirely sure of this). For now, I try to change the color from white to one I pick myself. I call the function in the MouseDown function, and I think it should work. Please help! bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height); private void FloodFill(int x, int y) { bmp.SetPixel(x, y, DrawColor); //sets the color of the pixel to DrawColor I picked pictureBox1.Image = bmp; // assigns the bitmap to the picture box; I work with it to be ale to save the picture if (bmp.GetPixel(x + 1, y) == Color.White) //all four search for each white pixel FloodFill(x + 1, y); if (bmp.GetPixel(x, y + 1) == Color.White) FloodFill(x, y + 1); if (bmp.GetPixel(x - 1, y) == Color.White) FloodFill(x - 1, y); if (bmp.GetPixel(x, y - 1) == Color.White) FloodFill(x, y - 1); return; } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint.X = e.X; startPoint.Y = e.Y; FloodFill(e.X, e.Y); pictureBox1.Image = bmp; drag = true; }

    T C D 3 Replies Last reply
    0
    • K k_crysa

      I have just started working in c# so I'm not that familiar with it. Still, I need to build a paint program and I am a bit stuck at Flood Fill. I want to be able to fill areas with an undetermined shape, so I used the SetPixel command, but it sets only one pixel (I'm not entirely sure of this). For now, I try to change the color from white to one I pick myself. I call the function in the MouseDown function, and I think it should work. Please help! bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height); private void FloodFill(int x, int y) { bmp.SetPixel(x, y, DrawColor); //sets the color of the pixel to DrawColor I picked pictureBox1.Image = bmp; // assigns the bitmap to the picture box; I work with it to be ale to save the picture if (bmp.GetPixel(x + 1, y) == Color.White) //all four search for each white pixel FloodFill(x + 1, y); if (bmp.GetPixel(x, y + 1) == Color.White) FloodFill(x, y + 1); if (bmp.GetPixel(x - 1, y) == Color.White) FloodFill(x - 1, y); if (bmp.GetPixel(x, y - 1) == Color.White) FloodFill(x, y - 1); return; } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint.X = e.X; startPoint.Y = e.Y; FloodFill(e.X, e.Y); pictureBox1.Image = bmp; drag = true; }

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      You will have to setup a loop so that more than one pixel is changed.

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

      1 Reply Last reply
      0
      • K k_crysa

        I have just started working in c# so I'm not that familiar with it. Still, I need to build a paint program and I am a bit stuck at Flood Fill. I want to be able to fill areas with an undetermined shape, so I used the SetPixel command, but it sets only one pixel (I'm not entirely sure of this). For now, I try to change the color from white to one I pick myself. I call the function in the MouseDown function, and I think it should work. Please help! bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height); private void FloodFill(int x, int y) { bmp.SetPixel(x, y, DrawColor); //sets the color of the pixel to DrawColor I picked pictureBox1.Image = bmp; // assigns the bitmap to the picture box; I work with it to be ale to save the picture if (bmp.GetPixel(x + 1, y) == Color.White) //all four search for each white pixel FloodFill(x + 1, y); if (bmp.GetPixel(x, y + 1) == Color.White) FloodFill(x, y + 1); if (bmp.GetPixel(x - 1, y) == Color.White) FloodFill(x - 1, y); if (bmp.GetPixel(x, y - 1) == Color.White) FloodFill(x, y - 1); return; } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint.X = e.X; startPoint.Y = e.Y; FloodFill(e.X, e.Y); pictureBox1.Image = bmp; drag = true; }

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        You should not be setting the picture in the picture box inside your recursive function. There's also flood fill methods in GDI, perhaps you can p/invoke one of them ?

        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

        1 Reply Last reply
        0
        • K k_crysa

          I have just started working in c# so I'm not that familiar with it. Still, I need to build a paint program and I am a bit stuck at Flood Fill. I want to be able to fill areas with an undetermined shape, so I used the SetPixel command, but it sets only one pixel (I'm not entirely sure of this). For now, I try to change the color from white to one I pick myself. I call the function in the MouseDown function, and I think it should work. Please help! bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height); private void FloodFill(int x, int y) { bmp.SetPixel(x, y, DrawColor); //sets the color of the pixel to DrawColor I picked pictureBox1.Image = bmp; // assigns the bitmap to the picture box; I work with it to be ale to save the picture if (bmp.GetPixel(x + 1, y) == Color.White) //all four search for each white pixel FloodFill(x + 1, y); if (bmp.GetPixel(x, y + 1) == Color.White) FloodFill(x, y + 1); if (bmp.GetPixel(x - 1, y) == Color.White) FloodFill(x - 1, y); if (bmp.GetPixel(x, y - 1) == Color.White) FloodFill(x, y - 1); return; } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint.X = e.X; startPoint.Y = e.Y; FloodFill(e.X, e.Y); pictureBox1.Image = bmp; drag = true; }

          D Offline
          D Offline
          dybs
          wrote on last edited by
          #4

          k_crysa wrote:

          I want to be able to fill areas with an undetermined shape

          As for "undetermined shape", do you have access to the points used to define/enclose the region? If so, you may be able to use the System.Drawing.Drawing2D.GraphicsPath [^] or System.Drawing.Region[^] classes to fill your area. At the bottom of each of these pages is a link to "Members" of each class, so you can see the functions and properties available. I seem to remember there being a simple "Fill()" function, but can't find the reference to it right now. For performance reasons, it's usually best to avoid setting individual pixels if at all possible. I could be wrong, I may be thinking of the FillMode, which might be different that what you're looking for. Anyone know if I'm on the right track here? Hope this helps Dybs

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups