Fontsize difference

T

tshad

I am running a program to put a captcha image on my string and am trying to
make sure all the characters fit.

The problem is the size seems to be wrong.

What I am doing is starting from the rectangles' height + 1 (not sure why I
would use that as a starting point) then I loop through subtracting the
value by 1 until the size I get back from MeasureString is less than the
rectangle width.

I seem to be ending up with a string way to small.

The rectangle size is 200w * 50h.

When I use 50, I start losing characters on my display ( I am always using 7
and sometimes I will end up with 5 or 4 - not sure which routine is dropping
them - I assume it is my GraphicsPath.AddString that is doing it).

If I use 45, it always seems to work fine.

But if I run the loop and let the program figure it out it gets the
following:

After rect.width = 200 rect.height = 50
Inside do loop fontSize = 50 size.Width = 327.3979 rect.Width = 200
Inside do loop fontSize = 49 size.Width = 320.85 rect.Width = 200
Inside do loop fontSize = 48 size.Width = 314.3021 rect.Width = 200
Inside do loop fontSize = 47 size.Width = 307.7541 rect.Width = 200
Inside do loop fontSize = 46 size.Width = 301.2061 rect.Width = 200
Inside do loop fontSize = 45 size.Width = 294.6582 rect.Width = 200
Inside do loop fontSize = 44 size.Width = 288.1102 rect.Width = 200
Inside do loop fontSize = 43 size.Width = 281.5622 rect.Width = 200
Inside do loop fontSize = 42 size.Width = 275.0143 rect.Width = 200
Inside do loop fontSize = 41 size.Width = 268.4663 rect.Width = 200
Inside do loop fontSize = 40 size.Width = 261.9184 rect.Width = 200
Inside do loop fontSize = 39 size.Width = 255.3704 rect.Width = 200
Inside do loop fontSize = 38 size.Width = 248.8225 rect.Width = 200
Inside do loop fontSize = 37 size.Width = 242.2745 rect.Width = 200
Inside do loop fontSize = 36 size.Width = 235.7266 rect.Width = 200
Inside do loop fontSize = 35 size.Width = 229.1786 rect.Width = 200
Inside do loop fontSize = 34 size.Width = 222.6306 rect.Width = 200
Inside do loop fontSize = 33 size.Width = 216.0827 rect.Width = 200
Inside do loop fontSize = 32 size.Width = 209.5347 rect.Width = 200
Inside do loop fontSize = 31 size.Width = 202.9867 rect.Width = 200
Inside do loop fontSize = 30 size.Width = 196.4388 rect.Width = 200

30 is way less that the 45 that works.

Am I doing something wrong here? I am using the "Century Schoolbook" font.

Here is the code to test the size - the test is done just before the "while"
statement.

****************************************
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);
***********************************************

Thanks,

Tom
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top