Getting Started With SkiaSharp With Xamarin.Forms

A couple of months back, I wrote about Using OxyPlot with Xamarin Forms, where we created a few graphs to show data. But, what if a situation arises where we have to draw something like in the following image?

Oxyplot doesn’t support this, but we have another open source .Net library to our rescue. It’s Skiasharp. It’s fully cross-platform with rich 2D graphics drawing API powered by Google’s Skia library. This post will be a step by step guide to creating an image the same as the one shown above using SkiaSharp library in a Xamarin.Forms app. So, let’s get started.

Create a new Xamarin.Forms project with PCL (I prefer PCL over Shared project but that’s just me you can use your preference too).

Right click on the Solution file and select Manage NuGet Packages For Solution…from the context menu. It will open NuGet-Solution.

Click on the Browse tab if it’s not selected by default and write SkiSharp in the search text-box. It will list all the SkiaSharp libraries available in NuGet.The one which is used for Xamarin.Forms is called View.Forms. Select and install it as shown in below image.

Once the NuGets are installed, we can add the SkiaSharp View to the XAML. Here, we’re adding a SKCanvasView, the CPU-based drawing surface. HTML developers can relate this to their canvas :). Also, we need to register the SkiaSharpassembly to use in XAML just like any other third party control.

In the above code, first we are initializing the Canvas by pulling the objects from SKPaintSurfaceEventArgsof the method and then clearing it so that whenever this event is executed, a fresh graphic is drawn, as shown in thr following lines of code:

//Initialize the Canvas

SKSurface vSurface = args.Surface;

SKCanvas vCanvas = vSurface.Canvas;

//Clear the Canvas

vCanvas.Clear();

The SKSurface is the layer that directs drawing commands from SkiaSharp onto the underlying native drawing area and SKCanvas is the layer that translates the drawing commands such as DrawRect and DrawPath into the appropriate commands for drawing on the current surface.

The next main item needed for drawing is SKPaint which is used to set the Color of the figure to be drawn, Fontsize (if text is to be written) and other properties needed for beautification of the graphic. In our method this is the main SKPaint object which is used to draw,

//Creating the Paint object to color the Items

SKPaint vBlackPaint = new SKPaint {

Color = SKColors.Black,

StrokeWidth = 5,

StrokeCap = SKStrokeCap.Round,

TextSize = 60

};

Then, we have used the common methods like DrawRect, DrawLine, DrawTextfor drawing the the Kundali image as shown in the beginning of the blog. The units used here are device independent units which are managed by Xamarin & SkiaSharp according to the screen resolution of the device.

This is how the output of the above code looks in Android Emulator.

The sample code given in this article is published on GitHub. This article has just touched the surface of using SkiaSharp library to create a simple Kundali structure. You can experiment more with it by going through the Xamarin.Forms SkiaSharp Documentation. Let me know if I have missed anything or if you need further examples or explanation.