0

I'm programming in c# (windows form) for image processing purpose. I have a Bitmap image. In my image I have closed curve which may be convex or concave. The boundary of curve is illustrated by a special color . I want to fill it by a filling color. I implement my method (some thing like flood fill) but I get stack over flow exception. How can write method some like this:

FillPoly(Bitmap bitmap, Color boundaryColor, Color fillingColor)

Note: I have AForge Net and Emgu CV libraries in my project. Any solution using these libraries will be accepted.

Babak.Abad
  • 2,839
  • 10
  • 40
  • 74

1 Answers1

0

The method itself

// 1. Graphics is more general than Bitmap
// 2. You have to provide points of the desired polygon/curve 
private static void FillPoly(Graphics graphics, 
                             Color boundary, 
                             Color fillingColor, 
                             params Point[] points) {
  if (null == graphics)
   throw new ArgumentNullException("graphics");

  using (SolidBrush brush = new SolidBrush(fillingColor)) {
    using (Pen pen = new Pen(boundary)) {
      //TODO: think over, do you want just a polygon
      graphics.FillPolygon(brush, points);
      graphics.DrawPolygon(pen, points);

      //... or curve
      // graphics.FillClosedCurve(brush, points);
      // graphics.DrawClosedCurve(pen, points);
   }
  }
}

its using:

   Bitmap bmp = new Bitmap(200, 200);

   using (Graphics g = Graphics.FromImage(bmp)) {
     FillPoly(g, Color.Blue, Color.Red,
       new Point(5, 5),
       new Point(105, 6),
       new Point(85, 95),
       new Point(125, 148),
       new Point(8, 150));
   }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215