Re: [STRING] retrieve the pixel length, MeasureString



teo wrote:
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.

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

Note:
if MeasureString is not a good approach I will change it with no problem.


--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • Re: ev.graphics
    ... >> Is there a method in the Graphic to wrap a string into a rectangle? ... >> If so is there a way to specify the width and let a Graphics method ... >> .MeasureString but get weird numbers for the width and height. ... We don't know the string you measured. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Showing a message to IE 5+ users (yes, the browser detection question again)
    ... Like XSLT to Opera 8.x, ... One fool may use a UA string test to server XSLT, ... HTML/CSS/javascript to a browser that could handle it if it got it. ... I do not recall "mozilla" in HTTP.USER_AGENT string would be ever ...
    (comp.lang.javascript)
  • Re: Pens are tricky, hard to assign (Colors too) inside of paint handler
    ... (the node manager has a list of lines), the three functions do not use ... Without knowing what exception you're getting, ... Graphics g = e.Graphics; ... it will convert 'sss' to upper case as a new string - ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Open Browser Maximized
    ... ByVal lpOperation As String, _ ... ShellExecute 0, "open", strURL, vbNullString, vbNullString, 3 ... except I would like the browser window to open maximized. ... Currently the browser opens in the previous windows state. ...
    (microsoft.public.excel.programming)
  • Re: Pens are tricky, hard to assign (Colors too) inside of paint handler
    ... Pen is somehow int while myobject.tPen uses ... I tried this below trick but it failed to compile ("Cannot assign to ... Graphics g = e.Graphics; ... string still immutable. ...
    (microsoft.public.dotnet.languages.csharp)

Loading