Home All Groups Group Topic Archive Search About
Author
22 Feb 2005 7:30 PM
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!
Author
22 Feb 2005 7:38 PM
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

Show quoteHide quote
"Rob T" <RTorcellini@DONTwalchemSPAM.com> wrote in message
news:ughL0SRGFHA.2156@TK2MSFTNGP10.phx.gbl...
> 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!
>
Are all your drivers up to date? click for free checkup

Author
22 Feb 2005 7:47 PM
Daniel Billingsley
try g.MeasureString()
(assuming g is the Graphics object)

Show quoteHide quote
"Rob T" <RTorcellini@DONTwalchemSPAM.com> wrote in message
news:ughL0SRGFHA.2156@TK2MSFTNGP10.phx.gbl...
> 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!
>
>
Author
22 Feb 2005 8:05 PM
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"


Show quoteHide quote
"Rob T" <RTorcellini@DONTwalchemSPAM.com> wrote in message
news:ughL0SRGFHA.2156@TK2MSFTNGP10.phx.gbl...
> 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!
>
Author
26 Feb 2005 4:24 AM
Raj Prakash
Hey Rob,

The Graphics.MeasureString() returns a SizeF object.
Ref:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDrawingGraphicsClassMeasureStringTopic.asp

Hope this helps...
Raj Prakash


Show quoteHide quote
"Rob T" <RTorcellini@DONTwalchemSPAM.com> wrote in message
news:eTH7YmRGFHA.2756@TK2MSFTNGP15.phx.gbl...
> Thanks Jay and Daniel....I knew it had to be something fairly simple...I
> was searching for something related to "size"...not "measure"
>
>
> "Rob T" <RTorcellini@DONTwalchemSPAM.com> wrote in message
> news:ughL0SRGFHA.2156@TK2MSFTNGP10.phx.gbl...
>> 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!
>>
>
>
Author
22 Feb 2005 8:09 PM
Herfried K. Wagner [MVP]
Rob,

"Rob T" <RTorcellini@DONTwalchemSPAM.com> schrieb:
> 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').

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Bookmark and Share