473,799 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Q] MeasureString results and mono-space fonts

Dear Group,

I have a question about the result from ‘graphics.Measu reString' when
using a mono-spaced font. Firstly I assume that Courier New font is
mono-spaced, ie each character takes up the same space when drawn on
the screen.

I am working on a program that requires displaying data in fixed width
font. I assumed that with ‘graphics.Measu reString' I could calculate
the width of the string when drawn in a specified font.

However as can be seen from my simple example, that I have attached,
where I have overridden the ‘OnPaint' method for a label, with a
mono-spaced font, that the width of two characters is not the
summation of two single characters.

In my program I determined that width of "WW" is not the same as "W" +
"W". From lines 62 – 65 of my example

"W" or "1" - 18.07 pixels wide
"WW" - 29.82 pixels wide
"WWWWWWWW" - 100.29 pixels wide

Why are the widths not exactly the same for each character?

I was hoping to use the information to render my display by moving to
specific places on the screen to render the string.

What am I missing about MeasureString and fonts?
Thanks again for any comments.
Stuart

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows. Forms.Form
{
private myLabel label;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{
this.label = new myLabel();
this.label.Loca tion = new System.Drawing. Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing. Size(168, 104);
this.label.TabI ndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing. Font("Courier New", 14.25F,
System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.label);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}
}
public class myLabel : Label
{
protected override void OnPaint(PaintEv entArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.Measur eString("1", font);
SizeF wone = graphics.Measur eString("W", font);
SizeF two = graphics.Measur eString("WW", font);
SizeF ten = graphics.Measur eString("WWWWWW WW", font);
}
}
}
Nov 16 '05 #1
2 3608
You should try using MeasureCharacte rRanges, as this provides more accurate
results.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Stuart Norris" <st**********@y ahoo.com.au> wrote in message
news:51******** *************** ***@posting.goo gle.com...
Dear Group,

I have a question about the result from 'graphics.Measu reString' when
using a mono-spaced font. Firstly I assume that Courier New font is
mono-spaced, ie each character takes up the same space when drawn on
the screen.

I am working on a program that requires displaying data in fixed width
font. I assumed that with 'graphics.Measu reString' I could calculate
the width of the string when drawn in a specified font.

However as can be seen from my simple example, that I have attached,
where I have overridden the 'OnPaint' method for a label, with a
mono-spaced font, that the width of two characters is not the
summation of two single characters.

In my program I determined that width of "WW" is not the same as "W" +
"W". From lines 62 - 65 of my example

"W" or "1" - 18.07 pixels wide
"WW" - 29.82 pixels wide
"WWWWWWWW" - 100.29 pixels wide

Why are the widths not exactly the same for each character?

I was hoping to use the information to render my display by moving to
specific places on the screen to render the string.

What am I missing about MeasureString and fonts?
Thanks again for any comments.
Stuart

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows. Forms.Form
{
private myLabel label;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{
this.label = new myLabel();
this.label.Loca tion = new System.Drawing. Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing. Size(168, 104);
this.label.TabI ndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing. Font("Courier New", 14.25F,
System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.label);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}
}
public class myLabel : Label
{
protected override void OnPaint(PaintEv entArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.Measur eString("1", font);
SizeF wone = graphics.Measur eString("W", font);
SizeF two = graphics.Measur eString("WW", font);
SizeF ten = graphics.Measur eString("WWWWWW WW", font);
}
}
}

Nov 16 '05 #2
Hi,

"Stuart Norris" <st**********@y ahoo.com.au> wrote in message
news:51******** *************** ***@posting.goo gle.com...
Dear Group,

I have a question about the result from 'graphics.Measu reString' when
using a mono-spaced font. Firstly I assume that Courier New font is
mono-spaced, ie each character takes up the same space when drawn on
the screen.

I am working on a program that requires displaying data in fixed width
font. I assumed that with 'graphics.Measu reString' I could calculate
the width of the string when drawn in a specified font.

However as can be seen from my simple example, that I have attached,
where I have overridden the 'OnPaint' method for a label, with a
mono-spaced font, that the width of two characters is not the
summation of two single characters.

In my program I determined that width of "WW" is not the same as "W" +
"W". From lines 62 - 65 of my example

"W" or "1" - 18.07 pixels wide
"WW" - 29.82 pixels wide
"WWWWWWWW" - 100.29 pixels wide
Try using a typographic StringFormat:

SizeF one = graphics.Measur eString("1", font, 0,
StringFormat.Ge nericTypographi c);
SizeF wone = graphics.Measur eString("W", font,0,
StringFormat.Ge nericTypographi c);
SizeF two = graphics.Measur eString("WW", font,0,
StringFormat.Ge nericTypographi c);
SizeF ten = graphics.Measur eString("WWWWWW WWWW", font, 0,
StringFormat.Ge nericTypographi c);
Don't know exactly why or how but according to msdn the 'default
StringFormat' includes some space before and after the string when using
measurestring.

HTH,
greetings


Why are the widths not exactly the same for each character?

I was hoping to use the information to render my display by moving to
specific places on the screen to render the string.

What am I missing about MeasureString and fonts?
Thanks again for any comments.
Stuart

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows. Forms.Form
{
private myLabel label;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{
this.label = new myLabel();
this.label.Loca tion = new System.Drawing. Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing. Size(168, 104);
this.label.TabI ndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing. Font("Courier New", 14.25F,
System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.label);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}
}
public class myLabel : Label
{
protected override void OnPaint(PaintEv entArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.Measur eString("1", font);
SizeF wone = graphics.Measur eString("W", font);
SizeF two = graphics.Measur eString("WW", font);
SizeF ten = graphics.Measur eString("WWWWWW WW", font);
}
}
}

Nov 16 '05 #3

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

Similar topics

3
7959
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to edit the page. so i put it here. i want to kwon if this can convert all wave file. is there other encodage than 8 or 16 bits for .wav files? any bug and comment are welcome --
11
661
by: Tony Baker | last post by:
In order for my company to go ahead and use .Net and C# (over java), I need to know how to install and run a C# ASP.Net application not only on Windows, but on Linux. I'm a Windows developer, currently writing a c# ASP.Net application. The last time I used Linux (or whatever you want to call it) was back at Uni 5 years ago. What I want to do, is to see if I can get my ASP.Net project working on Linux using mono (www.mono-project.com) -...
5
2285
by: fliversen | last post by:
Hello, does anyone have successfull experience with mono on Windows. I have download mono-1.0.1-gtksharp-1.0-win32-0.5.exe But to compile und run programs whitch use Gtk# is not possible: The result is error CS0006: Cannot find assembly `gtk-sharp.dll'
1
1351
by: Joannes Vermorel | last post by:
I am currently doing some CS research that requires to collect some "web page retrieval latency" statistical data. Basically I have a list of URLs, and I want to measure the retrieval time of those URLs. So far, it seems very simple to do : <code> DateTime beforeCall = DateTime.Now; myHttpRequest.GetResponse(); DateTime afterCall = DateTime.Now; </code>
1
5046
by: Yaron | last post by:
Hi, I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics object created by a Panel, and use the MeasureString() method, I get a different width than when I pass up the procedure a Graphics object created by a PrintDocument. The procedure is exactly the same, independent of which object called it, so why...
0
1373
by: Meetali Goel | last post by:
Hi all, I can't determine why I keep getting "NotSupportedException" when g.MeasureString() is called in the following code (.NET CE). I have spent several hours but I cant seem to find the solution. I would really appreciate it if someone could look at the code below. Label l = new Label();
5
6825
by: Marc | last post by:
I want to use Measurestring in a Class. In a form I write cInt(Me.CreateGraphics.Measurestring("Text", mFont).Width) What do I write in a class (I dont't have the Me.CreateGraphics there...) Marc
6
1605
by: Twig | last post by:
Hi, Can I use strictly for C# development without missing anything from MS? Twig
7
6416
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
6
10564
by: Rainer Queck | last post by:
Hello NG, I am trying to adjust the column width of a DataGridView to the Text in its column header. To do this I am using the Graphics.MeasureString method. Unfortunatly I alway get a too short string width. What is it I am doing wrong? Here the code I am using: private void MinimizeDgvColumns(DataGridView dgv)
0
9546
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
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
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
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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
5467
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
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.