You just need to eliminate the extra width. You can do this using the string format :
GdipStringFormatGetGenericTypographic()
You can also use:
float doubleWidth = g.MeasureString(text+text,...).Width; float singleWidth = g.MeasureString(text).Width; float textWidth = doubleWidth-singleWidth;
This will allow you to work with other languages ββsuch as Japanese.
In the code project, Pierre Anad's solution was to use MeasureCharacterRanges , which returns the area corresponding to the exact bounding box of the specified line:
static public int MeasureDisplayStringWidth(Graphics graphics, string text, Font font) { System.Drawing.StringFormat format = new System.Drawing.StringFormat (); System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, 1000, 1000); var ranges = new System.Drawing.CharacterRange(0, text.Length); System.Drawing.Region[] regions = new System.Drawing.Region[1]; format.SetMeasurableCharacterRanges (ranges); regions = graphics.MeasureCharacterRanges (text, font, rect, format); rect = regions[0].GetBounds (graphics); return (int)(rect.Right + 1.0f); }
Chibueze opata
source share