Click to See Complete Forum and Search --> : C# and GetTextExtent
Brian Lasota
February 19th, 2003, 06:32 PM
I am writing a control in C#. I would like to get the size of th text in pixels from the Graphics object. This would be similair to DC.GetTExtExtent in VC++.
I've tried MeasureString but it does not return me what I was expecting.
Does anyone know how to do this? Is there some mode I have to set to get MeasureString to work as I want?
pareshgh
February 19th, 2003, 08:32 PM
Hi, I have following code written for you which will center the text in a form.
void DrawWelcomeMessage(string []s)
{
PictureBox p = pictureBoxWelcome;
Image im = p.Image;
Graphics g = Graphics.FromImage(im);
System.Drawing.Font f = new Font("Microsoft Sans Serif",12);
int y = im.Height/2;
Brush b = new SolidBrush(Color.Red);
for ( int i=0; i<s.Length; i++)
{
if ( i == 1 ) y = y+40;
SizeF nSize = g.MeasureString(s[i],f);
int x = (int) (im.Width - nSize.Width)/2;
g.DrawString(s[i],f,b,x,y);
y =y + (int)(nSize.Height*0.75);
}
g.Dispose();
p.Invalidate();
}
hope it will help you in what you are seeking,
Paresh
Brian Lasota
February 19th, 2003, 08:42 PM
Not really. I'm not looking to center text in a form, I'm looking to compute the with and height in pixels that the text takes up.
For example in GetTextExtent in VC++ a value of "XXX" with a Arial font of size 10 would have a height of about 8 pixels and a width of about 12 - 15 pixels.
pareshgh
February 19th, 2003, 09:06 PM
that's what
SizeF nSize = g.MeasureString(s[i],f);
does. SizeF will give u size u want of the font. make sure u use that particular fonts.
check out that class properly.
there is another class also with it.
Paresh:o
Brian Lasota
February 19th, 2003, 09:21 PM
Thanks for the response.
What was happening was i had the call as follows
TextSize = pe.Graphics.MeasureString(sValue, hHeaderCell.Font, sValue.Lenth);
passing in the length seemed to mess up the values. Once I removed the length it works great.
Thanks again. Sorry about the misunderstanding
pareshgh
February 20th, 2003, 10:26 AM
Ah ! I SEE . so good that it got working.
enjoy
Paresh:D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.