Re: [STRING] retrieve the pixel length, MeasureString
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Fri, 31 Aug 2007 08:46:33 +0200
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
.
- Follow-Ups:
- References:
- Prev by Date: Server side component, events and AJAX
- Next by Date: Re: Basic design question for a distributed application - How to access application's data
- Previous by thread: [STRING] retrieve the pixel length, MeasureString
- Next by thread: Re: [STRING] retrieve the pixel length, MeasureString
- Index(es):
Relevant Pages
|
Loading