|
|
Here we experiment with text alignment:
- The top text relies entirely on TextHAlign and TextValign for
positioning. When no text position or size is specified, these
parameters position the text on the bitmap. HAlign and VAlign have
no effect because the text area is the entire bitmap.
- The middle text sets the text size. This means the text
rectangle is limited to the area identified by this size and the
HAlign and VAlign properties are used to position such area.
TextHAlign and TextValign position text within such area.
- The bottom text specifies text size and position. HAlign and
VAlign have therefore no use as the text area is fully positioned.
TextHAlign and TextVAlign will still position the text within its
area.
[C#]
using (Bitmap bm = new Bitmap(600, 400)) {
using (Graphics graphics = Graphics.FromImage(bm)) {
graphics.Clear(Color.Yellow);
Font font = new Font("Times", 24, FontStyle.Regular);
Brush brush = Brushes.Black;
Rectangle rect = bm.Bounds;
StringFormat fmt = new StringFormat();
fmt.LineAlignment = StringAlignment.Near;
fmt.Alignment = StringAlignment.Near;
graphics.DrawString("Top Near", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Center;
graphics.DrawString("Top Center", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Far;
graphics.DrawString("Top Far", font, brush, rect, fmt);
fmt.LineAlignment = StringAlignment.Center;
fmt.Alignment = StringAlignment.Near;
graphics.DrawString("Center Near", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Center;
graphics.DrawString("Center Center", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Far;
graphics.DrawString("Center Far", font, brush, rect, fmt);
fmt.LineAlignment = StringAlignment.Far;
fmt.Alignment = StringAlignment.Near;
graphics.DrawString("Far Near", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Center;
graphics.DrawString("Far Center", font, brush, rect, fmt);
fmt.Alignment = StringAlignment.Far;
graphics.DrawString("Far Far", font, brush, rect, fmt);
}
bm.Save(Server.MapPath("IG8_StringFormat_LineAlignment.png"));
}

IG8_StringFormat_LineAlignment.png
|