1

I'm trying to draw half and partial circles (all BLACK lines) on a bitmap.

My INTENDED result looks like this:
enter image description here

My CURRENT result looks like this:
enter image description here

I've tried so many different alternatives but it nevers looks right.

using (var b = new Bitmap(200, 100, PixelFormat.Format24bppRgb))
{
   using (var g = Graphics.FromImage(b))
   {
      g.FillRectangle(new SolidBrush(Color.LightGray), 0, 0, 200, 100);

      // RED COLOR   
      Rectangle rec = new Rectangle(-15, 50, 70, 100);
      g.DrawRectangle(new Pen(Color.Red, 1f), rec);
      g.DrawArc(new Pen(Color.Red, 3f), rec, 50, 100);

      // WHITE COLOR   
      Rectangle rec = new Rectangle(10, 50, 70, 70);
      g.DrawRectangle(new Pen(Color.White, 1f), rec);
      g.DrawEllipse(new Pen(Color.White, 3f), rec);
   }
}

But it always look totally wrong and after hours of playing with the numbers, I could not find a way to control the output.

Question:
Is there a simple way to design the 3 black lines on my INTENDED image in a graphic object using C# ??

Johnny
  • 601
  • 6
  • 18
SF Developer
  • 5,244
  • 14
  • 60
  • 106
  • 1
    "But it always look totally wrong" - what do you mean by "totally wrong" ? The code you provided has no connection to the image you uploaded - furthermore it does not compile - you forgot a semicolon after `Pen p = new Pen(Color.Black)`. Please provide the actual code you wrote, upload the picture it generates and comment on that picture what you actually wanted to be generated. – Eduard Dumitru May 15 '14 at 16:29
  • Well, the current pic is my intended result. I guess I can add my current result, sure. – Johnny May 15 '14 at 16:39
  • Your `var b` is in a using bracket, so it's getting disposed without getting used. The first black line looks like a circle, the other two look like squares with rounded corners, but angled slightly differently. Might help us if you clarify what this image is used for. – LarsTech May 15 '14 at 16:40
  • 2
    @Johnny Whose question is this? You or Eager to Learn's? – LarsTech May 15 '14 at 16:54
  • @LarsTech Mine... Off course I'm using the output before the closure of the last bracket but that's not the problem I'm having. The issue is how to draw those lines... – Johnny May 15 '14 at 16:58
  • @Johnny; some of your edits are getting rejected because it looks like one use (Eager to Learn) posted the question, and you, a different user, are making substantial edits to the question. – Ray May 15 '14 at 17:02
  • 1
    Just draw a complete circle, it will of course be clipped to the bitmap boundaries. Or use Graphics.Clip yourself. – Hans Passant May 15 '14 at 17:08
  • @HansPassant is DrawArc the right way to draw a circle? – Johnny May 15 '14 at 17:11
  • Johnny is working temporarily with us and I have now asked him to let me handle this question – SF Developer May 15 '14 at 17:24

2 Answers2

1

The easy way to achieve this is to draw three concentric circles and let clipping take care of the fact that two of them fall outside the drawing region.

The way to achieve the arc-based drawing you want is probably to start with the concentric circles (so you know you have the rects in the right places), and then change the DrawEllipse to DrawArc, setting the start and sweep angles to the right values.

Start angle is measured in degrees from the x axis (horizontal line towards the right of the circle's centre), so for the smaller arc you will need an angle approximately 305 degrees. From there you need it to draw for about 90 degrees. The outer arc will be similar, but a smaller arc, so it might go from about 330 degrees for a sweep of about 60 degrees.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
0

It seems the solution is to draw a rectangle outside the boundaries of the bitmap and use the graphic.DrawEllipse method to draw the curve line.

Here is a snippet of the working code:

    Pen pen = new Pen(Color.White);

    Rectangle rec = new Rectangle(-30, 50, 100, 100);
    g.DrawEllipse(pen, rec);
    rec = new Rectangle(-30, 10, 150, 150);
    g.DrawEllipse(pen, rec);
    rec = new Rectangle(-30, -30, 200, 200);
    g.DrawEllipse(pen, rec);

Many thanks to Hans Passant to point me to this line of thinking.

SF Developer
  • 5,244
  • 14
  • 60
  • 106