DrawString size?

R

Rob T

Is there a way to determine what the size of a GDI+ string output would be
prior to drawing?

For example, If I have something like this:
Dim fnt As New Font("Times", 12)
Dim drawFormat As New StringFormat
drawFormat.LineAlignment = StringAlignment.Center
drawFormat.Alignment = StringAlignment.Center
g.DrawString("ABC", fnt, New SolidBrush(color.black), X1, Y1, drawFormat)

This would draw ABC horizontally & vertically centered the X1,Y1 point. The
problem is that is may be drawn off the edge of the screen, especially if it
were a long string. It would be great to know what the rectangular size is
prior to drawing.

Thanks!
 
J

Jay B. Harlow [MVP - Outlook]

Rob,
You can use Graphics.MeasureString to get the size.
drawFormat.Alignment = StringAlignment.Center

Dim sz As Size = g.MeasureString("ABC", ...)
g.DrawString("ABC", fnt, New SolidBrush(color.black), X1, Y1, drawFormat)

Hope this helps
Jay
 
R

Rob T

Thanks Jay and Daniel....I knew it had to be something fairly simple...I was
searching for something related to "size"...not "measure"
 
H

Herfried K. Wagner [MVP]

Rob,

Rob T said:
g.DrawString("ABC", fnt, New SolidBrush(color.black), X1, Y1, drawFormat)

This would draw ABC horizontally & vertically centered the X1,Y1 point.
The problem is that is may be drawn off the edge of the screen, especially
if it were a long string. It would be great to know what the rectangular
size is prior to drawing.

'Graphics.MeasureString' (and maybe 'Graphics.MeasureCharacterRanges').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top