0

I have a facial recognition library working that gives me an array of rectangle. Right now I'm using this way to draw the rectangle.

foreach (Rectangle box in boxes)
{
     for (int x = box.X; x <= box.X + box.Width; x++)
     {
          for (int y = box.Y; y <= box.Y + box.Height; y++)
          {
               outputbmp.SetPixel(x, y, Color.FromKnownColor(KnownColor.Red));
          }
     }
}

enter image description here

I'm looking for something as simple as:

Ellipse ellipse = new Ellipse(box); //cast rect to ellipse
outputbmp.DrawEllipse(ellipse);

which will look something more like:

enter image description here

where the outline of the ellipse touching the rectangle corners.

Based on the approach I used above, it is easy to draw a rectangle but for ellipse, it would require me to know all the points in the ellipse. Just wonder if there's anything to make my life easier.

Mc Kevin
  • 962
  • 10
  • 31

1 Answers1

1

Don't try to draw directly to the bitmap, there is a higher level object you can create, called a Graphics that gives you all kinds of wonderful drawing tools. It will also be significantly faster than drawing pixel by pixel.

You can create a Graphics for a given Bitmap by calling Graphics.FromImage and passing in the bitmap. You must remember ti call Dispose on the Graphics though, or it will leak resources.

Once you have a Graphics instance for your bitmap you can call DrawEllipse and pass in the bounds exactly as you expect.

From MSDN:

private void DrawEllipseInt(Graphics g)
{
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);

    // Create location and size of ellipse.
    int x = 0;
    int y = 0;
    int width = 200;
    int height = 100;

    // Draw ellipse to screen.
    g.DrawEllipse(blackPen, x, y, width, height);
}
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76