Drawing vertical text in WPF using DrawingContext.

2019-06-17 18:20发布

I am doing some custom drawing using DrawingContext in WPF. I am using DrawingContext.DrawText for drawing strings. Now, at a place I want to draw the text vertically. Is there any option in the DrawingContext OR DrawText() function to draw text vertically?

3条回答
够拽才男人
2楼-- · 2019-06-17 18:48

You will have to use PushTransform and Pop methods of DrawingContext class.

DrawingContext dc; // Initialize this correct value
RotateTransform RT = new RotateTransform();
RT.Angle = 90
dc.PushTransform(RT)
dc.DrawText(...);
dc.Pop();
查看更多
Deceive 欺骗
3楼-- · 2019-06-17 18:51
DrawingContext dc;
Point textLocation
var RT = new RotationTransform(-90.0);

// You should transform the location likewise...
location = new Point(-location.Y, location.X);

dc.PushTransform(RT);
dc.DrawText(formattedText, location);

Sorry, I had to post this because I was hitting my head against a wall for fifteen minutes trying to figure this out. I don't want anyone else to go through that.

查看更多
趁早两清
4楼-- · 2019-06-17 19:15

Here is my solution: It is need to create rotate transform around the text origin, so we pass x and y to RotateTransform constructor

       ...
        // ft - formatted text, (x, y) - point, where to draw            
        dc.PushTransform(new RotateTransform(-90, x, y));
        dc.DrawText(ft, new Point(x, y));
        dc.Pop();
        ...
查看更多
登录 后发表回答