By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Using System.Drawing.Graphics MeasureString() in a console Applicaiton

P: n/a
I'm trying to use MeasureString() to determine the length in pixels of a
string

However to do I need a System.Drawing.Graphics object

to do this I need to create a System.Drawing.Graphics object for which there
is only
two constructors

System.Drawing.Graphics.FromHdc

and

System.Drawing.Graphics.FromHwnd

My application is a C# Console application and I don't have and Forms to
pass the handle
into.

Does anyone know of a way of me overcoming this problem? perhaps I can get
the DesktopWindow in C#?

public SizeF HowBigIsThisText(string myString)
{
Font MyFontObject = new Font("MS San Serif",8);
System.Draing.Graphics gr=System.Drawing.Graphics.FromHwnd(<Don't know
what handle to provide here>)

return gr.MeasureStringmyString,MyFontObject);
}

Many thanks in advance

Dr Paul Hoad
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Paul Hoad wrote:
I'm trying to use MeasureString() to determine the length in pixels
of a string

However to do I need a System.Drawing.Graphics object

to do this I need to create a System.Drawing.Graphics object for
which there is only
two constructors

System.Drawing.Graphics.FromHdc

and

System.Drawing.Graphics.FromHwnd

My application is a C# Console application and I don't have and
Forms to pass the handle
into.

Does anyone know of a way of me overcoming this problem? perhaps I
can get the DesktopWindow in C#?

public SizeF HowBigIsThisText(string myString)
{
Font MyFontObject = new Font("MS San Serif",8);
System.Draing.Graphics gr=System.Drawing.Graphics.FromHwnd(<Don't
know what handle to provide here>)

return gr.MeasureStringmyString,MyFontObject);
}

Many thanks in advance

Dr Paul Hoad


Just create a form, set its Visible to false, then use its CreateGraphics
method.

- Pete
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.