Link to home
Create AccountLog in
Avatar of jtran007
jtran007

asked on

c# get the width of char with given font

Hi,
How do I get the width of char with given font?

Thanks,
JT
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
If you need precision, you can use MeasureCharacterRanges.

Note that MeasureString adds aditional space.

But MeasureCharacterRanges is complex to use. It returns a Region for each CharacterRange and you need dispose it.

Dim cr2Xact(1) As CharacterRange
Dim sf_xact As New StringFormat(StringFormat.GenericTypographic)
cr2Xact(0).Length = l1
cr2Xact(1).First = p2
cr2Xact(1).Length = l2

sf_xact.SetMeasurableCharacterRanges(cr2Xact)
Dim rgn = ig.MeasureCharacterRanges(Descr, f, rb, sf_xact)
x3 = x0 + CInt(rgn(0).GetBounds(ig).Width)
x4 = x0 + CInt(rgn(1).GetBounds(ig).Width)
rgn(0).Dispose() : rgn(1).Dispose()



 
Avatar of jtran007
jtran007

ASKER

Thanks,
JT