0

I'm trying to write text inside circle (not centered by rectangle) alignment must be from line to line inside circle.

I have successfully drawn the circle and text. I did research on Stack Overflow and Google without success about putting text inside this circle.

private void Button1_Click(object sender, EventArgs e)
{
    System.Drawing.Graphics graphicsObj;
    graphicsObj = this.CreateGraphics();

    // Create font and brush.
    Font drawFont = new Font("Arial", 5);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    // Create point for upper-left corner of drawing.
    float x = 150.0F;
    float y = 50.0F;
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.FitBlackBox;

    graphicsObj.DrawEllipse(Pens.Red, 20, 20, 350, 350);

    graphicsObj.DrawString(richTextBox1.Text.ToString(), drawFont, drawBrush, x, y, drawFormat);
}

Looking for advise, not for exact code..

-> How to establish connection for text alignment based on rectangle. Expected output like in this image : https://imge.to/i/miFif

exactly same but I need in C# Win.Form -> Wrap text inside a circular div

Kuba Do
  • 155
  • 9
  • I'm not sure what you mean by "alignment must be from line to line inside circle". Do you mean you want the text centred vertically and horizontally in the centre of the circle, or something else? – steve16351 Aug 23 '19 at 12:35
  • @steve16351 - please check this image https://imge.to/i/miFif – Kuba Do Aug 23 '19 at 12:53
  • 1
    I don't think there is an easy solution to this :-( - You probably need to draw the text line by line with growing and shrinking widths.. – TaW Aug 23 '19 at 14:58
  • 1
    There's no built-in support for text rendering in anything else than a rectangular region. You need to break down your strings in Regions and draw each section that fits a Region inscribed in a circle at specific points inside the circunference. You can take the circular label you saw before: [Translucent circle with text](https://stackoverflow.com/a/51435842/7444103) and combine `StringFormat.SetMeasurableCharacterRanges` with `Graphics.MeasureCharacterRanges` to define the regions. – Jimi Aug 23 '19 at 15:48
  • 1
    You can see an example about `SetMeasurableCharacterRanges` and the calculation of Text Regions in the last part of the tests here: [How to highlight wrapped text in a control using the graphics?](https://stackoverflow.com/a/48257170/7444103) and here: [How to compute the correct width of a digit in pixels?](https://stackoverflow.com/a/54772134/7444103) – Jimi Aug 23 '19 at 15:48
  • 1
    @Taw, Your comment drive me to solution:) like :: 1. count how many chars i can put on each row, 2 create array of substrings, 3 print row by row __Thank You all for comments, i will put later on code for future needs – Kuba Do Aug 24 '19 at 07:47
  • Yes, that is the way to go. Note that in most fonts character widths can vary greatly and for nice results you should measure them (or whole words) with Graphics.MeasureString.. – TaW Aug 24 '19 at 07:50

1 Answers1

0

DrawString has an overload with format options:

...
DrawString(e.Cache, text, rect, 
  new StringFormat() {
    LineAlignment = StringAlignment.Center, 
    Alignment = StringAlignment.Center
  });
Peter Csala
  • 17,736
  • 16
  • 35
  • 75