Reputation: 167
I need to write using Graphics.DrawString in a print Document method , a string with vertical direction and I have a limitation on the string width , the problem is that the string is writen from left to right and I need that the first line be on the right I'm using the method below
SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1, StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);
Upvotes: 1
Views: 12374
Reputation: 1548
I adapted the code snippet from @LarsTech's answer and it worked for me as below:
strF = new StringFormat();
strF.Alignment = StringAlignment.Near;
strF.FormatFlags = StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);
The text will be drawn as if it is written from bottom to top, left-aligned.
Upvotes: 0
Reputation: 167
The solution is to use RotateTransform(90) and without using the StringFormatFlags.DirectionVertical here is the peace of code:
Rectangle rec = new Rectangle();
rec.Height = 2 * po.medF.Height;
rec.Width=100;
rec.X = 0;
rec.Y = 0;
SizeF s;
String str = "your Text";
StringFormat strf = new StringFormat();
strf.Alignment = StringAlignment.Center;
rec.X = 0;
rec.Y = 0;
e.Graphics.TranslateTransform(X1, Y1);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(str, po.medF, Brushes.Black, rec, strf);
e.Graphics.ResetTransform();
Upvotes: 0
Reputation: 81620
As Hans' commented, the RotateTransform
can be used to flip the string around:
strF.Alignment = StringAlignment.Far;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);
The TranslateTransform changes the origin of your coordinate system to the bottom right corner of your rec
rectangle, then the RotateTransform
flips it 180 degrees, and then the alignment of the string is changed to Far to place the string into the same place your original string was drawing.
Upvotes: 4
Reputation: 4708
You should look at this example:
http://msdn.microsoft.com/en-us/library/aa287525%28v=vs.71%29.aspx
It should do exactly what you're asking.
EDIT:
Possibly stupid way of drawing from right to left:
SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1,
StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
string[] strRightToLeft = str1.Split('\n');
Array.Reverse(strRightToLeft);
e.Graphics.DrawString(String.Concat(str1), po.defaultF, Brushes.Black, rec, strF);
Upvotes: 0