[STRING] retrieve the pixel length, MeasureString

T

teo

Hallo,

I'd like to retrieve the pixel length of a string.

------------------------

I decided to use MeasureString,
but I have a problem with the graphic "instance" of it.
I'm in a Sub
and
I have the "graphics" word underlined:


Private Sub CalculateTheWidth

Dim myFont As New Font("Arial", 10)
Dim stringsize as SizeF
stringsize = graphics.MeasureString("Hallo", myFont)

End Sub




Also (little Asp.net theory here)
I will be on a .aspx web Form and a text in a Label control
and
I'd like to use the 'CalculateTheWidth' sub there too
because on some browsers the autosizing property of Labels
is not available.

But it seems that the MeasureString method is not available on Asp.Net.
So this measuring task is more difficult.
How to solve this?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

teo said:
Hallo,

I'd like to retrieve the pixel length of a string.

------------------------

I decided to use MeasureString,
but I have a problem with the graphic "instance" of it.
I'm in a Sub
and
I have the "graphics" word underlined:


Private Sub CalculateTheWidth

Dim myFont As New Font("Arial", 10)
Dim stringsize as SizeF
stringsize = graphics.MeasureString("Hallo", myFont)

End Sub

You need an instance of a Graphics object. Just send the graphics object
that you use for drawing as an argument to the method.
Also (little Asp.net theory here)
I will be on a .aspx web Form and a text in a Label control
and
I'd like to use the 'CalculateTheWidth' sub there too
because on some browsers the autosizing property of Labels
is not available.

But it seems that the MeasureString method is not available on Asp.Net.
So this measuring task is more difficult.
How to solve this?

It's available, but you have to add a reference to System.Drawing.
However, it's not very useful in an ASP.NET application, as you are
measuring the string for the screen on the server, and it's displayed in
the browser on the client.

The fonts available on the client is unknown to the server, so the font
you are using to measure the string might not even be available on the
client. Also, sizing of the text is different in the browser and in the
windows system. Most browsers allow the user to change the size of the
text, and this is not known to the server either. Another thing that
affects the displayed size is the font smoothing used, which also is not
known to the server.

You can measure the string to get an idea of how large it will be in the
browser, but you can't expect to get an accurate value.
 
T

teo

I have the "graphics" word underlined:
You need an instance of a Graphics object. Just send the graphics object
that you use for drawing as an argument to the method.


Mmmhh.... what do you mean with "the graphic object that
you use" (I'm not drawing, I'm measuring) ?
Do you have a snippet of code (a step-by-step example)?
You can freely retouch mine if you like.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

teo said:
Mmmhh.... what do you mean with "the graphic object that
you use" (I'm not drawing, I'm measuring) ?
Do you have a snippet of code (a step-by-step example)?
You can freely retouch mine if you like.

I assumed that you were drawing the string somewhere. If you aren't
going to draw anything, why would you need to know how large the string
would be when drawn?

You can use the CreateGraphics method for any control to get a Graphics
object for the screen.
 
T

teo

I assumed that you were drawing the string somewhere. If you aren't
going to draw anything, why would you need to know how large the string
would be when drawn?

You can use the CreateGraphics method for any control to get a Graphics
object for the screen.

I got it on a Win Form (CreateGraphics worked)

But it doesn't work on a Web Form
(I have the blue zig-zag underline under the 'CreateGraphics' method,
the error says that 'CreateGraphics' is not a member of
System.Web.UI.WebControls.Label

Now I'm going in the Asp.Net ng,
but if you have the answer you can append it here.

Here my code:

Imports System.Drawing

Label1.Text = "Hallo"

Dim instance As Graphics = Label1.CreateGraphics()
Dim text As String = Label1.Text
Dim font As New Font("Arial", 10)
Dim returnValue As SizeF
returnValue = instance.MeasureString(text, font)

Debug.Print(returnValue.Width.ToString)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

teo said:
I got it on a Win Form (CreateGraphics worked)

But it doesn't work on a Web Form
(I have the blue zig-zag underline under the 'CreateGraphics' method,
the error says that 'CreateGraphics' is not a member of
System.Web.UI.WebControls.Label

Of course it isn't. As a web control is never drawn on the screen there
is no need for a Graphics object.
Now I'm going in the Asp.Net ng,
but if you have the answer you can append it here.

Here my code:

Imports System.Drawing

Label1.Text = "Hallo"

Dim instance As Graphics = Label1.CreateGraphics()
Dim text As String = Label1.Text
Dim font As New Font("Arial", 10)
Dim returnValue As SizeF
returnValue = instance.MeasureString(text, font)

Debug.Print(returnValue.Width.ToString)

If you are going to use MeasureString in ASP.NET, you have to create a
graphics object some other way, as you don't have any screen to draw on.
Look into the other factory methods in the Graphics class.
 
T

teo

Of course it isn't. As a web control is never drawn on the screen there
is no need for a Graphics object.

If you are going to use MeasureString in ASP.NET, you have to create a
graphics object some other way, as you don't have any screen to draw on.
Look into the other factory methods in the Graphics class.

on the asp.net NG they told me it will not work
because another reason:
the server doesn't know the client font,
so it isn't able to calculate any length

But I'm telling the server what font is, with
Dim font As New Font("Arial", 10)
so I'm a little dubious about the above given reason.

Assuming it is possible to use MeasureString on a aspx Web Form,
I'm really in trouble in finding a CreateGraphics method for any control
(furthermore I have a string, not a control; only later I put it on a
control);
have any hint for the web MeasureString task ?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

teo said:
on the asp.net NG they told me it will not work
because another reason:
the server doesn't know the client font,
so it isn't able to calculate any length

Yes, that's exactly what I explained in my first reply.
But I'm telling the server what font is, with
Dim font As New Font("Arial", 10)
so I'm a little dubious about the above given reason.

I also explained in my first reply that the size measurement isn't only
affected by the font, but also by how the text is drawn. In a web
application you have no control over how the text is drawn, and you
can't even find out how it's drawn.
Assuming it is possible to use MeasureString on a aspx Web Form,

As I already explained, you can use it, but you can't expect to get an
accurate measurement.
I'm really in trouble in finding a CreateGraphics method for any control

That's because web controls doesn't use Graphics objects, as I already
explained.
(furthermore I have a string, not a control; only later I put it on a
control);
have any hint for the web MeasureString task ?

I already gave you a hint to look into the other factory methods in the
Graphics class. Have you done that?
 
T

teo

I already gave you a hint to look into the other factory methods in the
Graphics class. Have you done that?

Ok, I went back to the msdn again

at a first look I diascarded the .FromHwnd method
because it required an handle
and I was not able to get any valid handle on a Web Form,
so I thought it was a method suitable to Win Form only.

But because you insisted and because after I filtered the msdn
(so to have the web documentation only)
the .FromHwnd method was still available there,
I went deeper with it.

I saw that even without passing a known value (as in VB6),
but simply declaring like this:

Dim myHwnd As IntPtr

the myHwnd variable was fully working;
then I created a graphic object
with this:

Dim returnValue As Drawing.Graphics
returnValue = Drawing.Graphics.FromHwnd(myHwnd)

and finally got the returnValue that supports the MeasureString method

So it seems I found the way.

Just guessing how myHwnd used with no assigned known value can work...
 
A

Armin Zingler

teo said:
I saw that even without passing a known value (as in VB6),
but simply declaring like this:

Dim myHwnd As IntPtr

the myHwnd variable was fully working;
then I created a graphic object
with this:

Dim returnValue As Drawing.Graphics
returnValue = Drawing.Graphics.FromHwnd(myHwnd)

and finally got the returnValue that supports the MeasureString
method

So it seems I found the way.

Just guessing how myHwnd used with no assigned known value can
work...

Now you have a handle to the desktop on the server. An uninitialized IntPtr
is zero which means it points to the Desktop. You still don't know how it
will look like on the client and how the size is on the client. You only
know how it looks on the server's Desktop.
Maybe you want to have a bitmap/jpeg as part of your web page. Then you
would know the size on the client because it is displayed as is. In order to
draw on it, the Graphics objects can be used (Graphics.FromImage). Otherwise
it's useless.


Armin
 

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