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

Pixelwidth of string with given font

How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

Thanx.
Jesper.
Nov 15 '05 #1
4 2013
Jesper,

You will want to use the MeasureString method on the Graphics instance
representing the device context that you want to display the string on. It
should give you the width, in pixels (for a screen) of the string you want
to draw on it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

Thanx.
Jesper.

Nov 15 '05 #2
SizeF oSize = new SizeF();

crFont = new Font("arial", 16, FontStyle.Bold);
Graphics g = get your graphics object
oSize = g.MeasureString("Your String to measure", crFont);

hth

Pete

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

Thanx.
Jesper.

Nov 15 '05 #3
n!
> How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.


I use Graphics.MeasureString, if you're not measuring within the paint call
you'll have to create a graphics object.

n!
Nov 15 '05 #4
Be sure to read this KB article to know the cavets of GDI+ text output.

http://support.microsoft.com/?id=307208

Then consider using MeasureCharacterRanges if you need more accurate
results.

example:
textFormat.SetMeasurableCharacterRanges(new CharacterRange[] { new
CharacterRange(0, this.Text.Length()) } );

Region characterRegion = graphics.MeasureCharacterRanges(this.Text,
this.Font, new RectangleF(0, 0, float.MaxValue, float.MaxValue),
textFormat)[0];

ByronCullen
ThunderTools

"Jesper" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

Thanx.
Jesper.

Nov 15 '05 #5

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

Similar topics

4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
3
by: ofer h | last post by:
hello all i have got a span inside a td of a table. i am trying to clip the string that exceeds the length of the td and then put three dots ("...") after it so it will show that the text was...
2
by: Benny Raymond | last post by:
Since I have no control over a treeview's scroll bars (i'm drawing icons and text to the right of the treenodes) I've decided to just add some special text to the end of my tree node text which I...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
0
by: Andrus | last post by:
I created RDL file containing TextBox with constant text. I used the following code to find TextBox <Widthtag value. However textbox width is too large. How to find exact width of string for...
3
by: Dom | last post by:
In part of my code, I need to know the rectangle that a string will fit into, if it is written in a given font and a given Device Context (DC may be an old term). Back in the days of "C" and...
7
by: teo | last post by:
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
12
by: Jens | last post by:
Hi everyone, I'm trying to build a simple dropdown menu for a website and i need to need a way to retrieve the top and left style attributes of an object. However it seems style attributes like...
14
by: Roedy Green | last post by:
Is there a shortcut way to define the default font family (and characteristics) to be applied to all styles? -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.