473,289 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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

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
1 9535
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yaron | last post by:
Hi, I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics...
1
by: Dan Kelley | last post by:
I have 2 projects - 1 Winform project that sends Udp messages using the UdpClient class when a button is clicked, and a Console application that listens for these Udp messages. If I try to use...
0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
8
by: Al Jones | last post by:
Okay, this is dumb.... In gwbasic I can print using "#####.####"; 12.987654 and that produces ,,,12.9876 (commas for spaces). I've looked at buildstring, and tostring but nothing I've seen...
4
by: John Swan | last post by:
Hello. I'm trying to create a simple program that amongst other things creates a thumbnail of an image (Bitmap) to a set size determined by the user in pixels? The problem is: All of the...
1
by: jjbutera | last post by:
There are a lot of examples out there, but I was hoping something could help me further. I'd like to draw text (unknown length) to an image, but then crop the image to the height and width of the...
10
by: Paul | last post by:
Hi, I was wondering if anyone knows if it is possible in C# given x,y coordinates of several straight lines to draw these on the screen and then save this as an image file, also adding text to the...
0
by: thirunavukarasukm | last post by:
Hai.... To print Html page using PrintDialog and Print Document i am created one windows appication.. the windows application have many pages in my booking page i have two buttons one...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.