How to properly align text with drawstring method
Contents
[
Hide
]
How to properly align text with drawstring method
Issue : How to properly align text with drawstring method.
Tips : To properly align text with drawstring method can be used presented below sample code.
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; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string baseFolder = dataDir; | |
string[] alignments = new[] { "Left", "Center", "Right" }; | |
foreach (var alignment in alignments) | |
{ | |
DrawString(baseFolder, alignment); | |
} | |
void DrawString(string baseFolder, string align) | |
{ | |
string fileName = "output_" + align + ".png"; | |
string outputFileName = Path.Combine(baseFolder, fileName); | |
string[] fontNames = new[] | |
{ | |
"Arial", "Times New Roman", | |
"Bookman Old Style", "Calibri", "Comic Sans MS", | |
"Courier New", "Microsoft Sans Serif", "Tahoma", | |
"Verdana", "Proxima Nova Rg" | |
}; | |
float[] fontSizes = new[] { 10f, 22f, 50f, 100f }; | |
int width = 1000; | |
int height = 1200; | |
using (System.IO.FileStream stream = | |
new System.IO.FileStream(outputFileName, System.IO.FileMode.Create)) | |
{ | |
//Create an instance of PngOptions and set its various properties | |
Aspose.Imaging.ImageOptions.PngOptions pngOptions | |
= new Aspose.Imaging.ImageOptions.PngOptions(); | |
//Set the Source for PngOptions | |
pngOptions.Source = new Aspose.Imaging.Sources.StreamSource(stream); | |
//Create an instance of Image | |
using (Aspose.Imaging.Image image | |
= Aspose.Imaging.Image.Create(pngOptions, width, height)) | |
{ | |
//Create and initialize an instance of Graphics class | |
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image); | |
//Clear Graphics surface | |
graphics.Clear(Aspose.Imaging.Color.White); | |
//Create a SolidBrush object and set its various properties | |
Aspose.Imaging.Brushes.SolidBrush brush | |
= new Aspose.Imaging.Brushes.SolidBrush(); | |
brush.Color = Color.Black; | |
float x = 10; | |
int lineX = 0; | |
float y = 10; | |
float w = width - 20; | |
var pen = new Pen(Color.Red, 1); | |
StringAlignment alignment = StringAlignment.Near; | |
switch (align) | |
{ | |
case "Left": | |
alignment = StringAlignment.Near; | |
lineX = (int)Math.Round(x, 0); | |
break; | |
case "Center": | |
alignment = StringAlignment.Center; | |
lineX = (int)Math.Round(x + w / 2f, 0); | |
break; | |
case "Right": | |
alignment = StringAlignment.Far; | |
lineX = (int)(x + w); | |
break; | |
} | |
var stringFormat = new StringFormat(StringFormatFlags.ExactAlignment); | |
stringFormat.Alignment = alignment; | |
foreach (var fontName in fontNames) | |
{ | |
foreach (var fontSize in fontSizes) | |
{ | |
var font = new Font(fontName, fontSize); | |
string text = String.Format("This is font: {0}, size:{1}", fontName, fontSize); | |
var s = graphics.MeasureString(text, font, SizeF.Empty, null); | |
graphics. | |
DrawString(text, font, brush, new RectangleF(x, y, w, s.Height), stringFormat); | |
y += s.Height; | |
} | |
graphics.DrawLine(pen, new Point((int)(x), (int)y), new Point((int)(x + w), (int)y)); | |
} | |
graphics.DrawLine(pen, new Point(lineX, 0), new Point(lineX, (int)y)); | |
// save all changes. | |
image.Save(); | |
} | |
} | |
File.Delete(outputFileName); | |
} |