Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calculate Text Length in Pixels

Posted on 2006-04-19
10
Medium Priority
?
9,545 Views
Last Modified: 2009-06-08
I'm going to dynamically build a custom control, and add it to a page.  I'm not sure which kind of control elements yet, because:

After I add the text to the control's text element,

I need a way to calculate the length of the text in the text element...

So I can then change the element's height, top and width.....

So that it only shows two lines (wrapped), and that it's bottom edge is at "set-able" position.

Any suggestions on how to calculate the text width of any appropriate text element would be welcome.

Thanks!

0
Comment
Question by:codequest
10 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 1700 total points
ID: 16494079
There is no sure way of calculating this width. You can do couple of things ...

1. Choose a font that all brwosers are probably going to support. E.g. Verdana, Arial.
2. Fix the size of the font.
3. Create a Graphics object (Bitmap.. see System.Drawing namespace). And then use that graphics object to calculate size of the string in pixels.

This could give you some approximate size.

I think you need to weight the benefots of doing this and the overhead of calculating this width. And then decide if its worth it.
0
 
LVL 2

Author Comment

by:codequest
ID: 16494193
Thanks for your message.  After researching this a bit more, I think that your solution is probably right.  

I recently learned how to create graphic objects, so that's a start.

If I don't like the overhead, then the fallback will be to use the method you suggest to develop some conversion values for the target fonts and font sizes, using an assortment of random phrases.   Then I can apply the conversion value to the character count and come up with an approximate pixel length.  Fortunately, the app is somewhat forgiving if the length is miscalculated (some truncation, and some un-used double lines, will be acceptable.)

One big help would be if you could indicate the variables that I need to use to get the size of the string from the bitmap object.   I could probably work backwards from those variables to build the routines.

In the mean time I'll do some experimentation to find those variables.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 16494206
There are no variables. There is a method in Graphics object to actually calculate size of the string. And that will give you the height and width of the string in pixels. I don't remeber the name of the method at the momemt. Look in the doucmentatio
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:codequest
ID: 16494210
Ok, cool.  I'll check it out.  Thanks!
0
 
LVL 2

Author Comment

by:codequest
ID: 16494363
I see the Bitmap.width property, though it seems that it's necessary to set the width (either by loading something into it, or just specifying it when new) before it can be DrawString'd into...and then the width is just what it was initially set to.

So while I can get the width, I'm not seeing how the DrawString method is going to change the width.

There is definitely a method in windows forms for calculating the height and width of a rendered string (I think it's called MeasureRendered, or something like that), but I don't see its equivalent in web forms.

0
 
LVL 23

Assisted Solution

by:naveenkohli
naveenkohli earned 1700 total points
ID: 16494524
The procedure is quite simple.

1. Create a graphics object. You can set the height and width of Bitmap object as high as you want. It would not matter because you are not actually going to show this bitmap anywhere. This height and width simply gives you enough surface are to work with.
2. Call MeasureString method using this graphics object. Two parameters of this method are string and font.

Thats pretty much you need.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 300 total points
ID: 16494525
There is a problem with web pages and fonts and measuring text--the font size can't really be predicted when you have the ability to specify font size as XX-Small and change the zoom factor through a browser (like Internet Explorer).

If everything is known (fixed font size), then you could do something like this (C#):

Font font = new Font("Arial", 28);
// need a graphics object to call the MeasureString method...
Bitmap bmp = new Bitmap(1, 1);
g = Graphics.FromImage(bmp);
int width = (int)g.MeasureString("Hello, world!", font).Width;
g.Dispose();

Bob
0
 
LVL 2

Author Comment

by:codequest
ID: 16494553
Thanks for inputs.  Those are great tips.  I'll check them out in the AM...they should get me there, at least to test this approach.

I appreciate the uncertainty about font size...in the business context for this app there should some control over how the page is display (but you never know...)
0
 
LVL 2

Author Comment

by:codequest
ID: 16500008
Works great.  Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question