tags:

views:

321

answers:

3

Hi,
I'm using a function to call for a piece of text to be rendered within an area. The basic working of the function is:

Dim measureSize as Size
Do
    myFont = new Font(myFont.Name, myFont.Size - 1, FontStyle.Regular, GraphicsUnit.Document)
    'Initial font size is set insanely high for the moment, during testing.
    'Low initial font size is not the problem.
    measureSize = g.MeasureString(myString, myFont)
Loop until (measuredSize.width < desiredSize.width AND measuredSize.height < desiredSize.height)

The problem with this is that MeasureString is adding a lot of whitespace around the string it is drawing, and the final font is being rendered far too small.

I'm sure I remember some argument one can fiddle with in order to remove all the padding from the MeasureString method, but my searches currently aren't turning up anything.

Does anyone know how to use MeasureString to measure the EXACT size of the string without any bordering at all?

+2  A: 

There is an article of CodeProject that bypasses this: http://www.codeproject.com/KB/GDI-plus/measurestring.aspx

Groo
A: 

You should have a try with TextRenderer.MeasureText, as it returns the size of the text rather than the size of the text if anti-aliased.

MSDN Article

Pondidum
+1  A: 

Pass StringFormat.GenericTypographic to MeasureString. I recommend against TextRenderer because it has problems with clear-type when rendering to an in-memory bitmap.

Using TextRenderer for measurement and DrawString for drawing will undoubtedly get you into an inconsistency sooner or later.

romkyns