1

I use drawstring method to draw text inside rectangle ,.. i wanted to rotate the text 180 degree so i used this code

   Rectangle displayRectangleh =
       new Rectangle(new Point((int)posh.X, (int)posh.Y), new Size(rectwdh, recth));

   StringFormat format1h = new StringFormat(StringFormatFlags.DirectionVertical);
   format1h.LineAlignment = StringAlignment.Center;
   format1h.Alignment = StringAlignment.Center;
   b = new SolidBrush(Color.Black);
   e.ChartGraphics.Graphics.TranslateTransform((float)rectwdh / 2 + posh.X, recth / 2 + posh.Y);
   e.ChartGraphics.Graphics.RotateTransform(180);
   e.ChartGraphics.Graphics.TranslateTransform(-((float)rectwdh / 2 + posh.X), -(recth / 2 + posh.Y));
            Font boldFonth = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold, GraphicsUnit.Pixel, 1, true);
   e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Black, 2), displayRectangleh);
   e.ChartGraphics.Graphics.DrawString("This is rotated long text test ", boldFonth, b, (RectangleF)displayRectangleh, format1h);
   e.ChartGraphics.Graphics.ResetTransform();

where posh.X and posh.Y (recatangle position) , rectwdh (rectangle width) and recth (rectangle height)

It works perfect for short text but for long text the new line will be above my old line see the next pic :

enter image description here

I even tried to add new line character \n but same result. my question : how to rotate the text 180 inside rectangle and end up with proper alignment ??

user1477332
  • 325
  • 2
  • 4
  • 20

1 Answers1

2

Never having even used the StringFormat APIs before, I pounded at it randomly until I found this:

StringFormat format1h = new StringFormat(StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft);

zeromus
  • 1,648
  • 13
  • 14
  • 1
    I can't Understand Why this Answer is accepted. – Shahram Banazadeh Sep 14 '21 at 15:21
  • Because It Works. – zeromus Sep 15 '21 at 19:11
  • if you mean this will change direction ,No it does not! StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft never changes direction to bottom top. Only transforms solve this problem. do you mean this must be combined with Transform Solution? – Shahram Banazadeh Sep 19 '21 at 01:07
  • Why don't you try it and see? – zeromus Sep 20 '21 at 02:53
  • I Have tried it , not working. solution with Transform works – Shahram Banazadeh Sep 22 '21 at 15:07
  • So it works, with Transform. – zeromus Sep 23 '21 at 18:32
  • I think you may be misunderstanding something here. The question has code that draws a string, and transforms in it. The question was: how do I make this work? I provided a one-line solution.This solution does not print the string. Therefore it is not a complete replacement for the question's code. Therefore it is a replacement for that same line in the question's code. After making that replacement you will see that the Transform is still there. HTH. – zeromus Sep 23 '21 at 18:41
  • No I know this is a replacement and what are string format flags. misunderstanding was The problem It self. I thought you say this replacement will change direction of text from top bottom to bottom top – Shahram Banazadeh Sep 23 '21 at 23:27