FontConverter doesn't work as expected

T

Tony Johansson

Hello!

Here I have a piece of code where code marked with 1 and 2 give identical
text but code marked with 3
doesn't give the same size of the text. Even if I change code marked with 3
from 12pt to 33pt the same size of text is displayed.
Have I missed something here.

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
// 1 Font f = new Font("Arial", 12, FontStyle.Bold);

// 2 FontFamily ff = new FontFamily("Arial");
// 2 Font f = new Font(ff, 12, FontStyle.Bold);

// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

g.DrawString("Hello, World!", f, Brushes.Blue, 10, 10);
}

//Tony
 
K

kndg

[...]
// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

You are using incorrect format for the converter to parse. It should be
formatted as:

Font f = (Font)converter.ConvertFromString("Arial, 12pt, style=Bold");
 
T

Tony Johansson

kndg said:
[...]
// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

You are using incorrect format for the converter to parse. It should be
formatted as:

Font f = (Font)converter.ConvertFromString("Arial, 12pt, style=Bold");

No you example doesn't give correct font because it's very different from
code marked with 1 and 2

//Tony
 
J

Jeff Johnson

[...]
// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

You are using incorrect format for the converter to parse. It should be
formatted as:

Font f = (Font)converter.ConvertFromString("Arial, 12pt, style=Bold");

No you example doesn't give correct font because it's very different from
code marked with 1 and 2

Care to define "different"?
 
T

Tony Johansson

Jeff Johnson said:
[...]
// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

You are using incorrect format for the converter to parse. It should be
formatted as:

Font f = (Font)converter.ConvertFromString("Arial, 12pt, style=Bold");

No you example doesn't give correct font because it's very different from
code marked with 1 and 2

Care to define "different"?

At least different size of the text.

//Tony
 
K

kndg

kndg said:
[...]
// 3 FontConverter converter = new FontConverter();
// 3 Font f = (Font)converter.ConvertFromString("Arial, 12pt,
FontStyle.Bold");

You are using incorrect format for the converter to parse. It should be
formatted as:

Font f = (Font)converter.ConvertFromString("Arial, 12pt, style=Bold");

No you example doesn't give correct font because it's very different from
code marked with 1 and 2

//Tony

Hi Tony,

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Font font1 = new Font("Arial", 12, FontStyle.Bold);

FontFamily ff = new FontFamily("Arial");
Font font2 = new Font(ff, 12, FontStyle.Bold);

FontConverter converter = new FontConverter();
Font font3 = (Font)converter.ConvertFromString("Arial, 12pt,
style=Bold");

g.DrawString("Hello, World!", font1, Brushes.Blue, 10, 10);
g.DrawString("Hello, World!", font2, Brushes.Blue, 10, 30);
g.DrawString("Hello, World!", font3, Brushes.Blue, 10, 50);

MessageBox.Show(String.Format("font1 == font2 : {0}",
font1.Equals(font2)));
MessageBox.Show(String.Format("font1 == font3 : {0}",
font1.Equals(font3)));
MessageBox.Show(String.Format("font2 == font3 : {0}",
font2.Equals(font3)));
}

All would print the same and the result of equality is all true.

Please note that, the converter is sensitive to culture information and
if your current culture is other than english, you should translate the
comma (',') character to the equivalance character of your culture.

Regards.
 
J

Jeff Johnson

Please note that, the converter is sensitive to culture information and if
your current culture is other than english, you should translate the comma
(',') character to the equivalance character of your culture.

The easiest thing to do is to create the desired font "normally" and then
use FontConverter.ConvertToString() to see what the output is. The input to
ConvertFromString() should look exactly the same.
 
K

kndg

The easiest thing to do is to create the desired font "normally" and then
use FontConverter.ConvertToString() to see what the output is. The input to
ConvertFromString() should look exactly the same.

Yes, and I found that some countries in Europe use semicolon (';')
instead of comma (','). That's probably the source of Tony's error. Or,
he could use ConvertFromInvariantString() to instruct the converter to
parse using invariant culture.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top