Character in a Box

J

Jacques Oberto

Hi All,

I would like to draw a string composed of a single "A"
character in the currrent font to fit as exactly as possible
in a user specified rectangle. Any clue welcome.
Thanks,

Jacques
 
P

Peter Duniho

Hi All,

I would like to draw a string composed of a single "A" character in the
currrent font to fit as exactly as possible
in a user specified rectangle. Any clue welcome.

For some information, please review this thread from a few years ago:
http://groups.google.com/group/micr...read/thread/a7240811ef4ffdc9/2f726482a4ea73b7

Additionally, depending exactly what you're trying to do, you may find it
simplest to draw the character at the largest size supported into a
Bitmap, and then draw that Bitmap instance scaled down to fit the
rectangle. It won't be quite as high-quality as using the font at the
correct size, but assuming you set the interpolation mode to high-quality,
it would be pretty good. The worst differences will show up at very small
sizes, but you may not care about that (either because you won't be
displaying it at very small sizes, or because at very small sizes, the
loss of quality isn't that important or noticable).

Pete
 
F

Family Tree Mike

Jacques said:
Hi All,

I would like to draw a string composed of a single "A" character in the
currrent font to fit as exactly as possible
in a user specified rectangle. Any clue welcome.
Thanks,

Jacques

To measure a string as a rectangle, you use the
Graphics.MeasureString(). An initial font size would be the height of
the rectangle, though there is a buffer around the character that you
need to be aware.
 
J

Jacques Oberto

I would like to draw a string composed of a single "A" character in the
For some information, please review this thread from a few years ago:
http://groups.google.com/group/micr...read/thread/a7240811ef4ffdc9/2f726482a4ea73b7

Additionally, depending exactly what you're trying to do, you may find it
simplest to draw the character at the largest size supported into a
Bitmap, and then draw that Bitmap instance scaled down to fit the
rectangle. It won't be quite as high-quality as using the font at the
correct size, but assuming you set the interpolation mode to high-quality,
it would be pretty good. The worst differences will show up at very small
sizes, but you may not care about that (either because you won't be
displaying it at very small sizes, or because at very small sizes, the
loss of quality isn't that important or noticable).

Thanks Peter.
I also found this path-based code which looks very promising (post #3):
http://bytes.com/topic/c-sharp/answers/213863-dynamically-resize-text

Jacques
 
J

Jacques Oberto

I would like to draw a string composed of a single "A" character in the
For some information, please review this thread from a few years ago:
http://groups.google.com/group/micr...read/thread/a7240811ef4ffdc9/2f726482a4ea73b7

Additionally, depending exactly what you're trying to do, you may find it
simplest to draw the character at the largest size supported into a
Bitmap, and then draw that Bitmap instance scaled down to fit the
rectangle. It won't be quite as high-quality as using the font at the
correct size, but assuming you set the interpolation mode to high-quality,
it would be pretty good. The worst differences will show up at very small
sizes, but you may not care about that (either because you won't be
displaying it at very small sizes, or because at very small sizes, the
loss of quality isn't that important or noticable).

Thanks Peter.
I also found this path-based code which looks very promising (post #3):
http://bytes.com/topic/c-sharp/answers/213863-dynamically-resize-text

Jacques
 
P

Peter Duniho

To measure a string as a rectangle, you use the
Graphics.MeasureString(). An initial font size would be the height of
the rectangle, though there is a buffer around the character that you
need to be aware.

Depending on the OP's needs, a better choice might be
TextRenderer.MeasureText(). That generally provides more precise
measurements, due to the lack of a formatting border around the measured
text.

Note that in either case, there is the issue that there is not actually a
direct correlation between the font size in points. Even the Font
constructors that allow you to specify the font-size in other
GraphicsUnits can sometimes produce inconsistent results. My comments in
the other thread that I referenced go into this question in a little more
detail.

Pete
 
P

Peter Duniho

I also found this path-based code which looks very promising (post #3):
http://bytes.com/topic/c-sharp/answers/213863-dynamically-resize-text

Yes, that could be an acceptable compromise between finding a precise font
size for your purposes and scaling a bitmap. Scaling a GraphicsPath could
produce higher-quality results than the bitmap scaling. You should be
sure to test the solution at a wide variety of combinations of initial and
scaled sizes; depending on the scaling algorithm used by the OS, you may
run into the same issues you'd run into simply calculating a scaled font
size for the purpose.

Pete
 
F

Family Tree Mike

Peter said:
Depending on the OP's needs, a better choice might be
TextRenderer.MeasureText(). That generally provides more precise
measurements, due to the lack of a formatting border around the measured
text.

Note that in either case, there is the issue that there is not actually
a direct correlation between the font size in points. Even the Font
constructors that allow you to specify the font-size in other
GraphicsUnits can sometimes produce inconsistent results. My comments
in the other thread that I referenced go into this question in a little
more detail.

Pete

That was a good thread to point them. I didn't see your post until
after my post.
 

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