Width of String

N

Nicholas Paldino [.NET/C# MVP]

Randy,

If you have a Graphics instance for where you are going to render the
string to, you can call the MeasureString method on the Graphics instance,
passing the string and getting the size of the rendered text (in the
specified font) in return.

Hope this helps.
 
I

Ian Semmel

How do you get a Graphics instance of (say) a printer before you start
printing ?


Nicholas Paldino said:
Randy,

If you have a Graphics instance for where you are going to render the
string to, you can call the MeasureString method on the Graphics instance,
passing the string and getting the size of the rendered text (in the
specified font) in return.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

randy1200 said:
Any ideas on how to get the width of a string in pixels?

Thanks,
Randy
 
P

Peter Duniho

How do you get a Graphics instance of (say) a printer before you start
printing ?

That's a very good question, one for which I don't have a pure .NET
answer. As near as I can tell, .NET does not include this very useful
functionality.

You have a couple of other options:

* Refrain from doing any layout that requires measuring strings or
other things based on the Graphics until you get the first page. This
won't allow you to do any pagination feedback in your application until
the user has printed or previewed the document, but it is a fairly simple
approach.

* Use p/invoke to call CreateDC() with the specific printer driver you
want to use, and then use the resulting DC to create a Graphics instance.
This allows you to get a printer Graphics instance any time you want, but
may lead to having to do other non-.NET stuff as well (for example, I
don't see a way to get the printer driver filename in .NET, so you might
have to use p/invoke to do that as well).

Pete
 

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