72 строки
2.1 KiB
Markdown
72 строки
2.1 KiB
Markdown
---
|
||
id: 21aa1668-c9a4-480b-ab86-9f7655776547
|
||
title: SkiaSharp
|
||
uti: com.xamarin.workbook
|
||
platforms:
|
||
- DotNetCore
|
||
packages:
|
||
- id: SkiaSharp
|
||
version: 1.60.0
|
||
---
|
||
|
||
# SkiaSharp Workbook
|
||
|
||
Welcome to SkiaSharp, the most awesome, cross-platform 2D-graphics engine. It is powered by the same engine that powers Android and Chrome.
|
||
|
||
After installing the `SkiaSharp` NuGet package, we must make sure that the references have been added:
|
||
|
||
```csharp
|
||
#r "SkiaSharp"
|
||
```
|
||
|
||
That being done, we can now add the typical `using` statements:
|
||
|
||
```csharp
|
||
using SkiaSharp;
|
||
```
|
||
|
||
Now, we can start coding. Here we create a simple 256x256 canvas:
|
||
|
||
```csharp
|
||
// create the bitmap that will hold the pixels
|
||
var bitmap = new SKBitmap(256, 256);
|
||
|
||
// create the canvas so that we can draw on thet bitmap
|
||
var canvas = new SKCanvas(bitmap);
|
||
|
||
// clear the canvas, so that it is fresh
|
||
canvas.Clear(SKColors.Transparent);
|
||
|
||
// appear in Workbooks
|
||
bitmap
|
||
```
|
||
|
||
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:
|
||
|
||
```csharp
|
||
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
|
||
};
|
||
```
|
||
|
||
Now that our canvas is all ready, we can start drawing. Here we are writing the word “SkiaSharp” in the middle:
|
||
|
||
```csharp
|
||
// 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);
|
||
|
||
// appear in Workbooks
|
||
bitmap
|
||
```
|
||
|
||
That’s it! So simple! And, best of all, this code can be used ANYWHERE!
|
||
|
||
Check out the code on GitHub: [https://github.com/mono/SkiaSharp](https://github.com/mono/SkiaSharp "mono/SkiaSharp"). |