SkiaSharp vs System.Drawing - part 2

This is the continuation of that story. I keep going to write about the differences between these two libraries.

While drawing a string into a graphics in System.Drawing, you can set alignments easily both vertical and horizontal. This will provide your text position in 9 different ways: Left-top, center-top, right-top, left-center, center, right-center, left-bottom, center-bottom, and right-bottom.

StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center; //Far,Center,Near
sf.Alignment = StringAlignment.Center; //Far,Center,Near

As you can see, we also set underlines of the text easily with the following font style property in System.Drawing:

Font font = new Font("Arial", 16, FontStyle.Underline); 
//Bold, Italic, Regular, Strikeout, and Underline

Now, we are trying to get the same results with SkiaSharp. We’ll create a 500*500 SKImageInfo, and create a surface. Then, we’ll write into the center of that canvas with the following code:

var paint = new SKPaint
{
Color = SKColors.Black,
TextSize = 16,
TextAlign = SKTextAlign.Center, // Left,Right,Center
};

That would be an acceptable output:

But if we want to customize that text as in the System.Drawing, unfortunately, we can not do it easily as in System.Drawing!

At first, as you see there is no underline property in SkiaSharp since 2017.
And then, changing SKTextAlign to Left or Right is setting the text to the left or right of the given start points(250, 250).

So, if you want to align a text horizontally in a restricted area, you have to calculate the length of your text. Moreover, if you want it vertically, you need to calculate the height of your text, and it depends on your TextSize(FontSize).

To be honest, it is highly difficult and redundant for me. So, I preferred System.Drawing to draw texts and SkiaSharp to draw images in my project.

You can look into my GitHub repository about this article.

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store