473,387 Members | 1,420 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,387 software developers and data experts.

[STRING] retrieve the pixel length, MeasureString

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?

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

Note:
if MeasureString is not a good approach
I will change it with no problem.
Aug 30 '07 #1
7 6388
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
Aug 31 '07 #2
teo
>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.

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.

Aug 31 '07 #3
teo wrote:
>>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.


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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 31 '07 #4
teo
On Fri, 31 Aug 2007 18:48:48 +0200, Göran Andersson <gu***@guffa.com>
wrote:
>teo wrote:
>>>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.


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.
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)

Aug 31 '07 #5
teo wrote:
On Fri, 31 Aug 2007 18:48:48 +0200, Göran Andersson <gu***@guffa.com>
wrote:
>teo wrote:
>>>>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.

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.

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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 31 '07 #6
teo
>>
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.
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 ?

Sep 1 '07 #7
teo wrote:
>>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.

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?

--
Göran Andersson
_____
http://www.guffa.com
Sep 1 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xah Lee | last post by:
# strings can be joined by +. print "this" + " that" # string can be multiplied print "this" *5 # substring extraction is done by appending a bracket # with begin and ending index a="this...
8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
0
by: Fritz | last post by:
Hi, My problem is getting a string of variable length from a unmanaged DLL, called by a managed DLL using IJW. In der legacy DLL is declared bool UnmanagerClass::GetData(std::wstring* pString)...
10
by: Mavenos | last post by:
Hi Web Masters, Just wondering wether you can help us to come up with some tokenize script. My problem is wanted to display a LONG content into a short para (by giving minimum letter lenght)...
1
by: Jim Carlock | last post by:
If I'm putting together a list of links and I want to get add each item one at a time inside a foreach($aNames as $sName) loop, is there an easy way to get the number of pixels that the $aName...
5
by: Daniel | last post by:
c# string size limit? length of string limit?
0
by: catchrohith | last post by:
hi all I hav to make a swf file with a sound attached. To determine the total no. of frames required I hav to determine the either the length/duration of the mp3 file or the size of that...
3
by: fussellja | last post by:
Is there a way of defining the maximum length of a string parameter to a web service in .Net? I assumed that there would be an Xml... attribute that I could apply to a string definition in the...
3
by: Jason Huang | last post by:
Hi, In C#, how do I retrieve a special sub string within a big string? e.g., the "...fdsajk...ABC123...fdfdskafdl... ..." string has 5000 characters, and I want to retrieve the string "123"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.