Working with Text and Fonts
Contents
[
Hide
]
Working with text and fonts using C# is easy with Aspose.Drawing for .NET. You can combine fonts and text styles to draw text during graphics drawing. This article shows how to:
- Format Text
- Draw Text
- Get Existing Fonts
- Use Text Hinting
Draw Text
In order to draw text using C#, the following steps can be used.
- Instantiate a Bitmap object
- Instantiate a Graphics object with the bitmap object
- Initialize a brush
- Use the DrawString method to draw text on the bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET | |
using System.Drawing; | |
using System.Drawing.Text; | |
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
graphics.Clear(Color.White); | |
Brush brush = new SolidBrush(Color.Black); | |
Pen pen = new Pen(Color.Blue, 1); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
Rectangle rectangle = new Rectangle(100, 100, 800, 600); | |
graphics.DrawRectangle(pen, rectangle); | |
graphics.DrawString(text, arial, brush, rectangle); | |
bitmap.Save("DrawText.png"); |
Format Text
In order to format text using C#, the following steps can be used.
- Instantiate a Bitmap object
- Instantiate a Graphics object with the bitmap object
- Define the necessary string format parameters such as string alignment and line alignment
- Use the DrawString method to draw formatted text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET | |
using System.Drawing; | |
using System.Drawing.Text; | |
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
graphics.Clear(Color.White); | |
StringFormat stringFormat = new StringFormat(); | |
stringFormat.Alignment = StringAlignment.Center; | |
stringFormat.LineAlignment = StringAlignment.Center; | |
Brush brush = new SolidBrush(Color.Black); | |
Pen pen = new Pen(Color.Blue, 1); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
Rectangle rectangle = new Rectangle(100, 100, 800, 600); | |
graphics.DrawRectangle(pen, rectangle); | |
graphics.DrawString(text, arial, brush, rectangle, stringFormat); | |
bitmap.Save("FormatText.png"); |
Text Hinting
Text hinting mode can be specified using the API. The following C# code shows how to set the grid fitting mode.
- Instantiate a Bitmap object
- Instantiate a Graphics object with the bitmap object
- Use the TextRenderingHint property of the Graphics object to specify the hinting mode
- Use the DrawText method to draw text with the defined mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET | |
using System.Drawing; | |
using System.Drawing.Text; | |
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
graphics.Clear(Color.White); | |
DrawText(graphics, "Arial", 100); | |
DrawText(graphics, "Times New Roman", 200); | |
DrawText(graphics, "Verdana", 300); | |
bitmap.Save("Hinting.png"); | |
void DrawText(Graphics graphics, string familyName, int y) | |
{ | |
Brush brush = new SolidBrush(Color.Black); | |
Font font = new Font(familyName, 10, FontStyle.Regular); | |
string text = "The quick brown fox jumps over the lazy dog. 0123456789 ~!@#$%^&*()_+-={}[];':\"<>?/,.\\¹`"; | |
graphics.DrawString(text, font, brush, 100, y); | |
} |
Installed Fonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET | |
using System.Drawing; | |
using System.Drawing.Text; | |
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
graphics.Clear(Color.White); | |
Brush brush = new SolidBrush(Color.Black); | |
InstalledFontCollection fonts = new InstalledFontCollection(); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
graphics.DrawString(fonts.Families.Length + " installed font families.", arial, brush, 100, 100); | |
for (int i = 0; i < 6 && i < fonts.Families.Length; ++i) | |
{ | |
graphics.DrawString(fonts.Families[i].Name, arial, brush, 100, (i + 2) * 100); | |
} | |
bitmap.Save("InstalledFonts.png"); |