Graphics conversion textrenderer

I am working on a custom control and I ran into a problem when TextRenderer is a bit surprising. In my OnPaint event, I apply a transform to a Graphics object to compensate for the scroll position as follows:

e.Graphics.Transform = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, this.AutoScrollPosition.X, this.AutoScrollPosition.Y); 

Then I pass the graphic to all the sub-elements of the control so that they draw on it. One of these elements is to draw a text string on the graphics surface. And here I have a problem. This line works correctly when scrolling:

  e.Graphics.DrawString(this.Text, this.Font, brush, new PointF(this.Rectangle.X, this.Rectangle.Y)); 

But when I use TextRenderer, I get a completely different result. Here is the text line that should draw the text:

 TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.Rectangle, this.TextColor, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.PreserveGraphicsTranslateTransform); 

I thought that these two lines should give the same result. But for some reason, the second one uses the graphic transform in different ways, and as a result, when I look at the control, all text lines move at different speeds than the rest of the elements on the drawing surface. Can someone explain to me why this is happening?

+3
c # gdi +
May 24 '11 at 7:49
source share
1 answer

Here is my best guess: TextRenderer.DrawText is based on GDI and therefore depends on the resolution. Graphics.DrawString is GDI + and therefore resolution independent. See also in this article .

Since you are saying that the texts “move at different speeds”, it is likely that the GDI call uses a different default resolution than the one your Graphics object has. This would mean that you would have to configure AutoScrollCoordinates to keep the difference between the resolution of the graphic object and the default resolution of GDI.

+10
May 24 '11 at 8:29
source share
— -



All Articles