Differences between Graphic.TextWidth

In the Paint Event of Canvas, a picture is created that matches the size of the text t.

'Paint Event
Var t as String = "-200008"
Var pic as Picture

Var textWidth as Double = g.TextWidth(t)
Var textHeight as Double = g.TextHeight

pic = new Picture(textWidth+1,textHeight+1)          'Picture mit den ausmassen des Textes erstellen

'copy parameters
pic.Graphics.FontName = g.FontName
pic.Graphics.FontSize = g.FontSize
pic.Graphics.FontUnit = g.FontUnit
pic.Graphics.Bold     = g.Bold
pic.Graphics.Italic   = g.Italic
pic.Graphics.CharacterSpacing = g.CharacterSpacing


Var w1 as Double = g.TextWidth(t)
Var w2 as Double = pic.Graphics.TextWidth(t)
break

But,
I don’t understand, why the values ​​of w1 (Graphics from Paint Event) and w2 (Graphics from Picture) are different.

Debugger

Difference in the Depth value ?

Picture default value is 72 (I think)
Windows default depth is 96 (or more depending of HiDPI value)…

I have tried:

pic.VerticalResolution = 96
pic.HorizontalResolution = 96

And

pic.VerticalResolution = 200
pic.HorizontalResolution = 200

it makes no difference to w1/w2

How can you tell that ?

And why are-you comparing the two Pictures ?

I try:

'Paint Event
Var t as String = "-200008"
Var pic as Picture

Var textWidth as Double = g.TextWidth(t)
Var textHeight as Double = g.TextHeight

pic = new Picture(textWidth+1,textHeight+1)          'Picture mit den ausmassen des Textes erstellen
pic.VerticalResolution = 96
pic.HorizontalResolution = 96

'copy parameters
pic.Graphics.FontName = g.FontName
pic.Graphics.FontSize = g.FontSize
pic.Graphics.FontUnit = g.FontUnit
pic.Graphics.Bold     = g.Bold
pic.Graphics.Italic   = g.Italic
pic.Graphics.CharacterSpacing = g.CharacterSpacing


Var w1 as Double = g.TextWidth(t)
Var w2 as Double = pic.Graphics.TextWidth(t)
'Var r1 as Double = g.
break

w1 and w2 have the same value as before at the break Point:
WithResolution96

I try to draw the Text to the new created Picture, but the text is clipped because the new Piture is to small for the text because the TextWidth for the original Graphics is different.to the Picture.Graphic. Why?

Looks like a Windows problem. The weird code shows w1 = w2 on macOS.

I try this tomorow on a Win7 machine. I’m currently testing on Win11.

Thx Beatrix and Emilie

Not a problem - a feature.

The difference is Direct2D vs GDI-compatible rendering.

More details in this thread: HowTo: use Text Rendering using GDI and/or Direct2D in Xojo 2018r3

1 Like

change that to:

pic = new Picture(textWidth+1,textHeight+1,32)

You saved my day! Thx

I tested (off line) yesterday your code and I get the same results).
Xojo 22r2 / Monterey 12.5.

Want a debug screen shot ?

Here it is:

Thx!