Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use DrawText with TextAlignment

0 views
Skip to first unread message

Jan Axelson

unread,
Dec 15, 2009, 11:05:44 AM12/15/09
to
This compiles:

dc.DrawText("my text", (Resources.GetFont
(Resources.FontResources.NinaB)), Colors.Black, 0, 0);

But I would like to center the text, and this gives an invalid
argument error:

dc.DrawText("my text", (Resources.GetFont
(Resources.FontResources.NinaB)), Colors.Black, 0, 0, 80, 70,
TextAlignment.Center, TextTrimming.None);

I've tried different values for width, height, TextAlignment, and
TextTrimming.

Suggestions welcome.

Ant

unread,
Dec 16, 2009, 10:14:02 AM12/16/09
to
I use font.ComputeExtent and ask the font for the width of the text

public static void DrawTextCenter(Bitmap bmp, string text, Font
font, Color color, int X, int Y, int Space)
{
int newWidth = 0;
int newHeight = 0;
font.ComputeExtent(text, out newWidth, out newHeight);
int offsetX = (Space - newWidth) / 2;
bmp.DrawText(text, font, color, X + offsetX, Y);
}

"Jan Axelson" wrote:

> .
>

Jan Axelson

unread,
Dec 17, 2009, 11:21:28 AM12/17/09
to
That's helpful, thanks.

Jan

0 new messages