Search code examples
c#winformsdrawstring

drawstring rotate long text 180


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 ??


Solution

  • 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);