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

[Q] MeasureString results and mono-space fonts

Dear Group,

I have a question about the result from ‘graphics.MeasureString' 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.MeasureString' 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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows.Forms.Form
{
private myLabel label;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.label = new myLabel();
this.label.Location = new System.Drawing.Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing.Size(168, 104);
this.label.TabIndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing.Font("Courier New", 14.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(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(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.MeasureString("1", font);
SizeF wone = graphics.MeasureString("W", font);
SizeF two = graphics.MeasureString("WW", font);
SizeF ten = graphics.MeasureString("WWWWWWWW", font);
}
}
}
Nov 16 '05 #1
2 3570
You should try using MeasureCharacterRanges, as this provides more accurate
results.

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

"Stuart Norris" <st**********@yahoo.com.au> wrote in message
news:51**************************@posting.google.c om...
Dear Group,

I have a question about the result from 'graphics.MeasureString' 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.MeasureString' 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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows.Forms.Form
{
private myLabel label;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.label = new myLabel();
this.label.Location = new System.Drawing.Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing.Size(168, 104);
this.label.TabIndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing.Font("Courier New", 14.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(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(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.MeasureString("1", font);
SizeF wone = graphics.MeasureString("W", font);
SizeF two = graphics.MeasureString("WW", font);
SizeF ten = graphics.MeasureString("WWWWWWWW", font);
}
}
}

Nov 16 '05 #2
Hi,

"Stuart Norris" <st**********@yahoo.com.au> wrote in message
news:51**************************@posting.google.c om...
Dear Group,

I have a question about the result from 'graphics.MeasureString' 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.MeasureString' 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.MeasureString("1", font, 0,
StringFormat.GenericTypographic);
SizeF wone = graphics.MeasureString("W", font,0,
StringFormat.GenericTypographic);
SizeF two = graphics.MeasureString("WW", font,0,
StringFormat.GenericTypographic);
SizeF ten = graphics.MeasureString("WWWWWWWWWW", font, 0,
StringFormat.GenericTypographic);
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace test
{
public class Form1 : System.Windows.Forms.Form
{
private myLabel label;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.label = new myLabel();
this.label.Location = new System.Drawing.Point(48, 72);
this.label.Name = "Label";
this.label.Size = new System.Drawing.Size(168, 104);
this.label.TabIndex = 0;
this.label.Text = "Hi";
this.label.Font = new System.Drawing.Font("Courier New", 14.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(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(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = base.Font;

SizeF one = graphics.MeasureString("1", font);
SizeF wone = graphics.MeasureString("W", font);
SizeF two = graphics.MeasureString("WW", font);
SizeF ten = graphics.MeasureString("WWWWWWWW", font);
}
}
}

Nov 16 '05 #3

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

Similar topics

3
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...
11
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,...
5
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:...
1
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...
1
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...
0
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...
5
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...) ...
6
by: Twig | last post by:
Hi, Can I use strictly for C# development without missing anything from MS? Twig
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
6
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.