Hot to get Widht an Height of a charatter in a specified font

M

Massimiliano

Hi all.
How I can retreive a dimension in pixel of a character draw with
graphics.drawstring(...).

For example:

imgFont = New Bitmap(48, 48) ' for now the dimension is fixed
g = Graphics.FromImage(imgFont)
g.DrawString(ch, fontChoose, Brushes.Black, 0, 0)

How I can give a exatly dimension to the bitmap for contain a single
character withouth spacing?
Thanks
Modena Massimiliano
 
E

eBob.com

Do a Google search for Graphics.MeasureString and/or
Graphics.MeasureCharacterRanges. In my experience, if you want a precise
answer, this is a rather complicated area.

Good Luck, Bob
 
M

Mike Williams

How I can retreive a dimension in pixel of a character draw
with graphics.drawstring(...). For example:
imgFont = New Bitmap(48, 48) ' for now dimension is fixed
g = Graphics.FromImage(imgFont)
g.DrawString(ch, fontChoose, Brushes.Black, 0, 0)
How I can give a exatly dimension to the bitmap for contain
a single character withouth spacing?

Not quite sure what you mean by "without spacing"? Do you mean you want just
the area actually occupied by the character glyph itself excluding any
surrounding white space within the character cell? If so then you'll
probably need to use the GetGlyphOutline GDI function using GGO_METRICS as
the uFormat parameter, which will return a GlyphMetrics structure for you
containing, amongst other things, gmBlackBoxX, gmBlackBoxY and
gmptGlyphOrigin which tell you the size and position within the character
cell of the smallest rectangle that completely encloses the glyph.
Incidentally these things, as with most things concerning fonts, are device
dependent and the values returned even for the same font and character will
often be different on different displays and printers, depending mostly on
their dpi resolution. By the way, there may actually be a vb.net native
method to get this stuff for you, but I wouldn't know about because I
actually use only VB6 and I just happened to be nosing around in here :)

Mike
 
M

Mike Williams

Michel Posseth [MCP] wrote:
Did you already tried the measure string method
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx

the measureString function is perfect! Thanks a lot!
Massimiliano

As a matter of interest, does that return the width of the actual drawn
glyph or the width of the character cell containing the glyph or perhaps the
width of the character cell plus a little extra for any possible overhang? I
haven't got vb.net installed and so I can't try it myself, but I'm
interested nonetheless in the way it natively handles fonts. In your
original post you asked how to find the width of a single character "without
spacing" and I asked whether you meant the width of the glyph without its
surrounding character cell white space, but you did not answer. For example,
if you draw in a Times New Roman 100 point font in Italic what does the
measurestring function return (in pixels) for the character "f" and for the
character "." (the decimal point or full stop or period or whatever you call
it)?

Mike
 
M

Mike Williams

.. . . further to my recent post asking what value the measurestring function
returns in pixels for the 100 point Times New Roman Italic characters "f"
and "." I naturally meant when those characters are drawn to a standard
display (which usually runs at 96 pixels per logical inch) but if you are
using a display running at some other dpi resolution (perhaps 120 dpi or
something else) then I'd need to know the curent dpi resolution of your
machine as well as the reported pixel font widths. Hope you don't mind
checking this for me.

Mike
 
M

Massimiliano

Mike said:
. . . further to my recent post asking what value the measurestring
function returns in pixels for the 100 point Times New Roman Italic
characters "f" and "." I naturally meant when those characters are drawn
to a standard display (which usually runs at 96 pixels per logical inch)
but if you are using a display running at some other dpi resolution
(perhaps 120 dpi or something else) then I'd need to know the curent dpi
resolution of your machine as well as the reported pixel font widths.
Hope you don't mind checking this for me.

Mike
I don't say how change the dpi of my screen.
the 'f' char is 82*164 pixel @ 96dpi. the '.' is 79*164pixel.
The bitmap generated it's for an embebbed sistem that draw a bitmap for
each char. I don't need any vectorial sistem.
 
M

Mike Williams

the 'f' char is 82*164 pixel @ 96dpi. the '.' is 79*164pixel
[in each case using 100 point Times New Roman Italic]

Right. Thanks a lot. It looks as though the MeasureString method is
returning the height and width of the rectangular character cell including
the white space, otherwise the width of the italic "." character would be
very much less than the width of the italic "f" character. Also, because in
both cases the height is about right but the width is about twice what I
would have expected it looks as though the MeasureString method is adding
some extra pixels to account for the fact that some characters have a
considerable underhang and overhang and that it is adding those pixels
whether or not the actual character at either the beginning or the end of
the string you are measuring actually does have either underhang or
overhang. Also, since MeasureString is apparently suitable for your needs, I
think I can safely assume that your answer to my original question when I
asked whether by "without spacing" you meant that you wanted to measure just
the size of the glyph itself would be "No".
The bitmap generated it's for an embebbed sistem that draw a
bitmap for each char. I don't need any vectorial sistem.

When I said that you might like to use the GDI GetGlyphOutline function I
didn't actually mean that you should use it to obtain the vector graphic
"shape" of the character (although it is of course capable of performing
such a task). I meant that you might like to use it to return the width and
height of the "black box" that exactly encloses the drawn "glyph", which is
another use for the function, because I wasn't sure what you were after when
you said that you wanted the size of a character "without the space". As it
turns out, and something which I didn't know at the time, you are after the
character size purely to create a bitmap of a suitable size for drawing as a
bitmap in an embedded system, in which case the size of the glyph is very
definitely not what you want, and the character cell size is what you
require.

Thank you for the feedback.

Mike
 
Top