473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Colo r.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromIm age(tb);
bmpGraphics.Cle ar(Color.White) ;

bmpGraphics.Dra wString(sometex t, 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 11031
DOH! :D

font.GetHeight

"Lasse Edsvik" <la***@nospam.c om> wrote in message
news:uY******** ********@TK2MSF TNGP12.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(Colo r.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromIm age(tb);
bmpGraphics.Cle ar(Color.White) ;

bmpGraphics.Dra wString(sometex t, 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(Colo r.Red);

Bitmap tb = new Bitmap(200,60);

Graphics bmpGraphics = Graphics.FromIm age(tb);
bmpGraphics.Cle ar(Color.White) ;

bmpGraphics.Dra wString(sometex t, 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.Measur eString 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.Measur eCharacterRange s

--
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
10742
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 explaination for this? A co-worker told me it is not valid HTML. He may be right, but we both agree; it's odd that IE would handle it when the form is loaded, but not
2
16640
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 (square) images in e.g. the top bar, which should fill out the whole height of the bar, it depends on the screen resolution, wether they look like square images or rectangles.
15
122147
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: 123px; height: 456px;" alt="">
2
6797
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
3465
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 = document.getElementById("Content"); var leftOne = document.getElementById("leftContent "); content.style.height = (leftOne.offsetHeight + leftOne.offsetTop) + "px"; return true;
8
9269
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 first rollover. It seems that the image height and width become set to zero after the image is displayed once; or perhaps that the height:expression(eval(...)); code is executed only once for each page load/reload. problem demo:
16
4480
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 specified.". I don't understand this result. Should I modify something ? Thanks
3
5315
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 (or something like that). I have a vague recollection that "" did have a defined meaning at some point in the past, but maybe I am mis-remembering. Can anyone confirm? I have encountered this in some third-party PHP code that I'm using on
7
9865
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 W3C HTML 4.01 standard and this group's archive didn't give me a satisfactory answer, so here we go: Is <img src="img.png" alt="" width="100px" height="100px"really valid?
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.