2021-08-06 03:09:17 +03:00
#!markdown
# SkiaSharp
2017-06-24 03:02:42 +03:00
Welcome to SkiaSharp, the most awesome, cross-platform 2D-graphics engine. It is powered by the same engine that powers Android and Chrome.
2024-04-28 12:56:01 +03:00
The first thing we need to do is install the package:
2021-08-06 03:09:17 +03:00
#!csharp
#i https://aka.ms/skiasharp-eap/index.json
#r nuget:SkiaSharp,2.80.*-*
2017-06-24 03:02:42 +03:00
2021-08-06 03:09:17 +03:00
#!markdown
2017-06-24 03:02:42 +03:00
2021-08-06 03:09:17 +03:00
After installing the `SkiaSharp` NuGet package, we can now add the typical `using` statements:
#!csharp
2017-06-24 03:02:42 +03:00
using SkiaSharp;
2021-08-06 03:09:17 +03:00
#!markdown
2017-06-24 03:02:42 +03:00
Now, we can start coding. Here we create a simple 256x256 canvas:
2021-08-06 03:09:17 +03:00
#!csharp
2017-06-24 03:02:42 +03:00
// create the bitmap that will hold the pixels
var bitmap = new SKBitmap(256, 256);
2021-08-06 03:09:17 +03:00
// create the canvas so that we can draw on that bitmap
2017-06-24 03:02:42 +03:00
var canvas = new SKCanvas(bitmap);
// clear the canvas, so that it is fresh
canvas.Clear(SKColors.Transparent);
2021-08-06 03:09:17 +03:00
#!markdown
2017-06-24 03:02:42 +03:00
Before we can draw anything, we need to create the object that will be used to describe how the thing we are drawing will look. To do this, we need a `SKPaint` object:
2021-08-06 03:09:17 +03:00
#!csharp
2017-06-24 03:02:42 +03:00
var paint = new SKPaint {
IsAntialias = true, // smooth text
TextSize = 50, // 50px high text
TextAlign = SKTextAlign.Center, // center the text
Color = 0xFF3498DB, // Xamarin light blue text
Style = SKPaintStyle.Fill, // solid text
Typeface = SKTypeface.FromFamilyName("Trebuchet") // use the Trebuchet typeface
};
2021-08-06 03:09:17 +03:00
#!markdown
2017-06-24 03:02:42 +03:00
Now that our canvas is all ready, we can start drawing. Here we are writing the word “SkiaSharp” in the middle:
2021-08-06 03:09:17 +03:00
#!csharp
2017-06-24 03:02:42 +03:00
// clear the canvas, just in case we are running this a second time
canvas.Clear(SKColors.Transparent);
// draw the text using the paint
canvas.DrawText("SkiaSharp", 128, 128 + (paint.TextSize / 2), paint);
2021-08-06 03:09:17 +03:00
// appear in VS Code
2017-06-24 03:02:42 +03:00
bitmap
2021-08-06 03:09:17 +03:00
#!markdown
2017-06-24 03:02:42 +03:00
That’ s it! So simple! And, best of all, this code can be used ANYWHERE!
2021-08-06 03:09:17 +03:00
Check out the code on GitHub: [https://github.com/mono/SkiaSharp](https://github.com/mono/SkiaSharp "mono/SkiaSharp").