Formatting Text with C# and ASP.NET Framework
Font myFont = new Font("Arial", 12, FontStyle.Bold);
Alternatively you can use FontFamily and set font in this way
FontFamily myFontFamily = new FontFamily("Arial");
Font myFont = new Font(myFontFamily, 12);
Writing text is also simple and you can do it in two ways by creating Font and Brush objects or simply by creating System.Drawing.Brushes property
Graphics myGraphics = this.CreateGraphics();
Font myFont = new Font("Arial", 40, FontStyle.Bold);
myGraphics.DrawString("Hello, World!", myFont, Brushes.Blue, 10, 10);
There are several ways you can manipulate text. Most important are listed here:
- Alignment - gets and sets text alignment horizontally with options of setting text in the Center, Near or Far.
- Format Flags - gets and sets flags that contain formatting info such as DirectionTightToLeft, DirectionVertical, DisplayFormatControl, FitBlackBox, LineLimit, MeasureTrailingSpaces, NoClip, NoFontFallback, NoWrap.
- LineAlignment - gets and set vertical text alignment with options of Center, Near, Far.
- Trimming - gets and sets string trimming with possible options such as Character, EllipsisCharacter, EllipsisPath, EllipsesWord, None and Word.
// Construct a new Rectangle.
Rectangle myStringFormat = new Rectangle(new Point(40, 40), new Size(80, 80));
// Construct 2 new StringFormat objects
StringFormat myStringFormat = new StringFormat(StringFormatFlags.NoClip);
StringFormat myStringFormat1 = new StringFormat(myStringFormat);
// Set the LineAlignment and Alignment properties for
// both StringFormat objects to different values.
myStringFormat.LineAlignment = StringAlignment.Near;
myStringFormat.Alignment = StringAlignment.Center;
myStringFormat1.LineAlignment = StringAlignment.Center;
myStringFormat1.Alignment = StringAlignment.Far;
myStringFormat1.FormatFlags = StringFormatFlags.DirectionVertical;
// Draw the bounding rectangle and a string for each
// StringFormat object.
myGraphics.DrawRectangle(Pens.Black, myStringFormat);
myGraphics.DrawString("Format1", this.Font, Brushes.Red, (RectangleF)myStringFormat, myStringFormat);
myGraphics.DrawString("Format2", this.Font, Brushes.Red, (RectangleF)myStringFormat, myStringFormat1);