PC Review


Reply
Thread Tools Rate Thread

DrawString size?

 
 
Rob T
Guest
Posts: n/a
 
      22nd Feb 2005
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!


 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      22nd Feb 2005
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

"Rob T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>



 
Reply With Quote
 
Daniel Billingsley
Guest
Posts: n/a
 
      22nd Feb 2005
try g.MeasureString()
(assuming g is the Graphics object)

"Rob T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
Rob T
Guest
Posts: n/a
 
      22nd Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      22nd Feb 2005
Rob,

"Rob T" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
Raj Prakash
Guest
Posts: n/a
 
      26th Feb 2005
Hey Rob,

The Graphics.MeasureString() returns a SizeF object.
Ref:
http://msdn.microsoft.com/library/en...tringTopic.asp

Hope this helps...
Raj Prakash


"Rob T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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!
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the appropriate font size for DrawString method? =?Utf-8?B?R2FyeQ==?= Microsoft C# .NET 2 4th Aug 2006 04:13 PM
Determining Size of Graphics.DrawString robfarmergt@gmail.com Microsoft Dot NET Compact Framework 1 3rd Aug 2006 01:31 PM
DrawString size? Rob T Microsoft Dot NET Framework 5 26th Feb 2005 04:24 AM
Re: DrawString Robert Misiak Microsoft C# .NET 3 13th Jul 2004 02:44 AM
DrawString Caeb Microsoft Dot NET Framework 0 3rd May 2004 07:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:21 AM.