GraphicsPath.GetBounds() not working with Strings

Staeff

New member
Joined
Jan 22, 2004
Messages
4
Hi,
Im trying to get the Bounds for my Text in a path, but its not working.
I wrote following code according to the MSDN Example for a Ellipse..

PHP:
pth = new GraphicsPath();
p=new Pen(Sermon.TextColor[j],1.0f);
pth.AddString("This is just a text", new FontFamily("Arial"),0,20,new Point(10,10),StringFormat.GenericTypographic);
graphics.DrawPath(p,pth);
MessageBox.Show(Convert.ToString(pth.GetBounds()));

I can see the Text, but the Messagebox Shows "{X=0,Y=0,Width=0,Heigth=0}" . I tried it also with an Ellipse and this worked fine..

Any suggestions?
 
You can use the [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemdrawinggraphicsclassmeasurestringtopic1.htm]MeasureString method[/mshelp] of the graphics object to
determine the size of the string drawn with a certain font.

Also, the tags for code are
Code:
 and [code=csharp] for VB and C#, respectively.
 
Thanks a lot for the Tip!
But theres a little problem:

GraphicsPath.AddString draws somehow smaller Strings than graphics.DrawString. MeasureString returns the Drawstring Size, Ill try to do a proportional conversion, but thats no nice way..
 
I was having a very similar problem putting outlined text on a button (button1). The MeasureString just didnt seem able to return the correct size. This is what I did to correct my problem...

// From previous post by Bob Powell
e.Graphics.TextRenderingHint=TextRenderingHint.AntiAlias;
e.Graphics.SmoothingMode=SmoothingMode.AntiAlias;

// Measure String for text Alignment (Vert & Horz)
String sButtonText = "Process Transaction...";
SizeF sz = e.Graphics.MeasureString(sButtonText,button1.Font);

// Let the StringFormat handle the Centering of text
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

// Fancy pants outlined text
GraphicsPath pth = new GraphicsPath();
pth.AddString(sButtonText,
button1.Font.FontFamily,0,button1.Font.Size,
new RectangleF(0,0,button1.Width,button1.Height),
format);

//draw the hollow outlined text
e.Graphics.DrawPath(new Pen(button1.ForeColor),pth);

pth.Dispose();

Hope this helps.
 
Back
Top