How to apply graphics scale and translate to TextRenderer

I use the scaling and transformation of my graphic when drawing a custom control to apply scaling and scrolling. I am using the following:

Matrix mx = new Matrix(); mx.Scale(mZoomFactor, mZoomFactor); mx.Translate(-clip.X + mGraphicsOffsetx, -clip.Y + mGraphicsOffsety); e.Graphics.Clip = new Region(this.Bounds); e.Graphics.Transform = mx; 

Then when I draw the lines using:

 Graphics g = ... g.DrawString(...) 

Scaling and transformation are correctly applied to strings, they are reduced and included, etc.

However, if for drawing strings I use the following:

 TextRenderer.DrawText(...) 

Text is not scaled and converted correctly.

Do you know how to apply these concepts to a TextRenderer ?

+6
source share
1 answer

The comments above are accurate - TextRenderer.DrawText , being a GDI, has limited support for coordinate conversion, given its resolution. As you noticed, coordination translation is supported, but there is no scaling (and none of them is coordinate).

The solution we used (and the only resource I could find on the Internet) was to manually scale the Font and Rectangle objects to reflect the scaling applied by Matrix.Scale(float, float) in combination with Graphics.Transform :

  private Font GetScaledFont(Graphics g, Font f, float scale) { return new Font(f.FontFamily, f.SizeInPoints * scale, f.Style, GraphicsUnit.Point, f.GdiCharSet, f.GdiVerticalFont); } private Rectangle GetScaledRect(Graphics g, Rectangle r, float scale) { return new Rectangle((int)Math.Ceiling(rX * scale), (int)Math.Ceiling(rY * scale), (int)Math.Ceiling(r.Width * scale), (int)Math.Ceiling(r.Height * scale)); } 

Here is the whole test form:

 public partial class Form1 : Form { private PictureBox box = new PictureBox(); public Form1() { InitializeComponent(); this.Load += new EventHandler(Form1_Load); } public void Form1_Load(object sender, EventArgs e) { box.Dock = DockStyle.Fill; box.BackColor = Color.White; box.Paint += new PaintEventHandler(DrawTest); this.Controls.Add(box); } public void DrawTest(object sender, PaintEventArgs e) { Graphics g = e.Graphics; string text = "Test Text"; float scale = 1.5F; float translate = 200F; var flags = TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.PreserveGraphicsTranslateTransform; var mx = new Matrix(); mx.Scale(scale, scale); mx.Translate(translate, translate); g.Clip = new Region(Bounds); g.Transform = mx; Size rendererPSize = Bounds.Size; Font f = GetScaledFont(g, new Font("Arial", 12), scale); Size rendererRSize = TextRenderer.MeasureText(g, text, f, rendererPSize, flags); Rectangle rendererRect = new Rectangle(0, 0, rendererRSize.Width, rendererRSize.Height); Rectangle r = GetScaledRect(g, rendererRect, scale); TextRenderer.DrawText(g, text, f, realRect, Color.Black, flags); } private Font GetScaledFont(Graphics g, Font f, float scale) { return new Font(f.FontFamily, f.SizeInPoints * scale, f.Style, GraphicsUnit.Point, f.GdiCharSet, f.GdiVerticalFont); } private Rectangle GetScaledRect(Graphics g, Rectangle r, float scale) { return new Rectangle((int)Math.Ceiling(rX * scale), (int)Math.Ceiling(rY * scale), (int)Math.Ceiling(r.Width * scale), (int)Math.Ceiling(r.Height * scale)); } } 
+6
source

Source: https://habr.com/ru/post/905906/


All Articles