Main Site Documentation

Measure the width of a string (drawn using Bitmap.DrawText)


#1

Hi,

Is there a way to measure the width of a string drawn with Bitmap.DrawText ?
In the full framework there’s Graphics.MeasureString … is there something like this in the MF ?


#2

DrawText returns the width of text in pixels


#3

Hey,

I just checked… it returns void …
Also… if I want to know the width of the text so I can create a Bitmap instance with that width.

Are we talking about the same class.


#4

Ìn the past it was supported though:

[ObsoleteAttribute]
public int DrawText (
         string text,
         int xPos,
         int yPos,
         Font font
)

So they must have provided a new way to do it right?


#5

Yes, you are right. That one is obsolete. Try ComputeExtent from the Font class:

http://msdn.microsoft.com/en-us/library/ee431962.aspx


#6

Thnx!

That makes more sense as well
as now you can measure the text before you create the bitmap to hold it.

Did not think to look there.

Life saver!