How to Draw String with extra distance between symbols or lines
Contents
[
Hide
]
How to Draw String with extra distance between symbols or lines
Issue : How to Draw String with extra distance between symbols or lines.
Tips : To Draw String with extra distance between symbols or lines there can be used StringFormat.Alignment, StringFormat.LineAlignment and StringFormatFlags options of stringformat object.
Example :
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
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Tiff; | |
using Aspose.Imaging.FileFormats.Tiff.Enums; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
//Horizontal left to right | |
DrawText(StringAlignment.Near, 0); | |
DrawText(StringAlignment.Far, 0); | |
DrawText(StringAlignment.Center, 0); | |
//Horizontal right to left | |
DrawText(StringAlignment.Near, StringFormatFlags.DirectionRightToLeft); | |
DrawText(StringAlignment.Far, StringFormatFlags.DirectionRightToLeft); | |
DrawText(StringAlignment.Center, StringFormatFlags.DirectionRightToLeft); | |
//Vertical left to right | |
DrawText(StringAlignment.Near, StringFormatFlags.DirectionVertical); | |
DrawText(StringAlignment.Far, StringFormatFlags.DirectionVertical); | |
DrawText(StringAlignment.Center, StringFormatFlags.DirectionVertical); | |
void DrawText(StringAlignment alignment, StringFormatFlags flags) | |
{ | |
string baseFolder = dataDir; | |
string fileName = "output_" + alignment + "_" + flags + ".png"; | |
string outputFileName = Path.Combine(baseFolder, fileName); | |
int[] fontSizes = new[] { 8, 12, 16, 24, 32, 48, 64, 96 }; | |
using (MemoryStream ms = new MemoryStream()) | |
using (Image bmp = Image.Create(new BmpOptions() { Source = new StreamSource(ms) }, 500, 900)) | |
{ | |
Graphics gr = new Graphics(bmp); | |
gr.Clear(Color.White); | |
gr.PageUnit = GraphicsUnit.Pixel; | |
string text = "Hello world 1111 \n2222"; | |
Brush brush = new SolidBrush(Color.Black); | |
float ypos = 0; | |
for (int i = 0; i < fontSizes.Length; i++) | |
{ | |
int fontSize = fontSizes[i]; | |
Font font = new Font("Times New Roman", fontSize); | |
StringFormat format = new StringFormat() | |
{ | |
CustomCharIdent = new PointF(5, 5), | |
Alignment = alignment, | |
FormatFlags = flags | |
}; | |
RectangleF rectangle = new RectangleF(0, ypos, 200, fontSize * 2.5f); | |
gr.DrawString(text, font, brush, rectangle, format); | |
ypos += rectangle.Height * 1.2f; | |
gr.DrawRectangle(new Pen(Color.Red), rectangle); | |
} | |
bmp.Save(outputFileName, new PngOptions()); | |
} | |
File.Delete(outputFileName); | |
} |