473,326 Members | 2,095 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,326 software developers and data experts.

Get "width" and "height" of text?

Hello

I was wondering if its possible to the "width" and "height" of a text when
creating a pic from text.

string sometext = "bla bla";
Font font = new Font("Verdana", 18);

SolidBrush sb = new SolidBrush(Color.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromImage(tb);
bmpGraphics.Clear(Color.White);

bmpGraphics.DrawString(sometext, font, sb, 14, 10);

in this case i create a 200x60, but I would like to create one that is as
wide and high as the text.

is that possible? even when changing font?

TIA
/Lasse
Nov 16 '05 #1
3 10983
DOH! :D

font.GetHeight

"Lasse Edsvik" <la***@nospam.com> wrote in message
news:uY****************@TK2MSFTNGP12.phx.gbl...
Hello

I was wondering if its possible to the "width" and "height" of a text when
creating a pic from text.

string sometext = "bla bla";
Font font = new Font("Verdana", 18);

SolidBrush sb = new SolidBrush(Color.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromImage(tb);
bmpGraphics.Clear(Color.White);

bmpGraphics.DrawString(sometext, font, sb, 14, 10);

in this case i create a 200x60, but I would like to create one that is as
wide and high as the text.

is that possible? even when changing font?

TIA
/Lasse

Nov 16 '05 #2
Lasse Edsvik wrote:
Hello

I was wondering if its possible to the "width" and "height" of a text
when creating a pic from text.

string sometext = "bla bla";
Font font = new Font("Verdana", 18);

SolidBrush sb = new SolidBrush(Color.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromImage(tb);
bmpGraphics.Clear(Color.White);

bmpGraphics.DrawString(sometext, font, sb, 14, 10);

in this case i create a 200x60, but I would like to create one that
is as wide and high as the text.

is that possible? even when changing font?

TIA
/Lasse


There is a MeasureString method that returns an approximate (!) size.

Hans Kesting
Nov 16 '05 #3
Hi Lasse,

As Hans said you can use Graphics.MeasureString which calculates the size of a string based on a certain font.
It is not 100% accurate but I believe it will return at least the size and then a tiny bit more.
If you want accurate size you need to use Graphics.MeasureCharacterRanges

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

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

Similar topics

4
by: John MacIntyre | last post by:
Hi, Using the script below, when you click on the radio button beside click here .... the first row shrinks. Has anybody else ever experienced this? Does anybody know of a logical...
2
by: Georg Goerg | last post by:
hello, i want to make website with a top navigation bar and one on the left. so by using css i define three layers and everyone of them has length attr. in percents. but if i want to put...
15
by: Gérard Talbot | last post by:
Hello all, I'd like to know and understand the difference between, say, <img src="/ImageFilename.png" width="123" height="456" alt=""> and <img src="/ImageFilename.png" style="width:...
2
by: Thomas Wang | last post by:
I draw a combobox on a form, but I can not change its height, What I can do about it? B.Rgds
2
by: Dag Sunde | last post by:
I have the following function to resize a div element (Content) depending on the size of another div element (leftConent). function adjustContentHeight() { var content =...
8
by: fredo | last post by:
This question was asked in comp.lang.javascript with no result. In IE5.x and IE6, I want to display an image when the user rolls over a text link. The image does indeed display, but only on the...
16
by: Tantale | last post by:
I used this serviec to check my webpage http://www.jmrw.com/Abroad/Barcelone/index.htm Made with Dreamweaver 8. The result is 206 errors, most of them "end tag omitted, but OMITTAG NO was...
3
by: Phil Endecott | last post by:
Dear Experts, It looks as if the HTML4 spec does not define a meaning for empty height and width attributes in an IMG element. Moz seems to ignore them, while IE7 sets the dimension to 1 pixel...
7
by: Christian Hackl | last post by:
Hi everyone, I've got a question about what makes the "img" element's width/height attributes valid HTML or XHTML. First of all, this is a rather theoretical question, but digging through the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.