How do you apply a Scale Translation to a DrawingC

2019-07-14 08:59发布

问题:

I have a Canvas with a scale translation applied in XAML. Using DrawingContext I draw lines on the Canvas. I now need to add text to the screen. I thought with formatted text I could apply a translation, but neither Formatted Text or DrawingContext accepts RenderTransform. How do I apply a scale translation to the text so it will counter the scale translation of the canvas?

 ftext = New FormattedText("N", CultureInfo.GetCultureInfo("en-us"), Windows.FlowDirection.LeftToRight, face, Me.DBFontSize, FalconDataBlock.Foreground)
 context.DrawText(ftext, .TargetLineInfo.EndAsWinPoint)

回答1:

In most UI apps, instead of using a DrawingContext, the right thing to do might be to add a child TextBlock to the Canvas, and change the TextBlock's RenderTransform.

However, assuming you have a good reason for using a DrawingContext, you could use DrawingContext.PushTransform to push the transform you need (scale, translation, etc.), just before you invoke DrawText. Use DrawingContext.Pop to restore the prior state after you're done with DrawText.