Generate Thumbnail/Cover With SkiaSharp

Cover on this website is generated using SkiaSharp Only

Requirements

  • Visual Studio Code (VS Code)
  • Polyglot Notebook Extension
    • Create New File
  • A little bit of experience in C#
  • For Basic/Installation please visit Basic Setup

Code

Install SkiaSharp
1
#r "nuget:SkiaSharp"

Import SkiaSharp Library

1
using SkiaSharp;

Code To Generate Cover Or Thumbnail

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int width = 640;
int height = 220;  
int marginY = -10;
int marginX = -10;

string Numbering ="1";
string mainText ="SKIA SHARP";
string subText = "Generate Thumbnail | Polyglot Notebook";

string backGroundColor ="#003366";
string textColor = "#ffffff";
string boderColor = "#ffffff";
SKBitmap bmp = new(width, height);
SKCanvas canvas = new(bmp); 
canvas.Clear(SKColor.Parse(backGroundColor)); 
using (var paint = new SKPaint()) 
{
    paint.TextSize = width/ 10.0f;
    paint.IsAntialias = true;
    paint.Color = SKColor.Parse(textColor);
    paint.IsStroke = false;
    paint.StrokeWidth = 3; 
    paint.TextAlign = SKTextAlign.Center; 
    canvas.DrawText(mainText, width / 2f, height / 2f, paint);
    paint.TextSize = width/ 25.0f;
    paint.TextAlign = SKTextAlign.Right;
    canvas.DrawText(subText, width+marginX, height+marginY, paint);
    paint.TextSize = width/ 20.0f;
    canvas.DrawText(Numbering, (width/ 20.0f)*Numbering.Length,(width/ 20.0f)*1.25f, paint);
    paint.IsStroke = true;
    paint.TextAlign = SKTextAlign.Center;
    paint.Color = SKColor.Parse(textColor); 
}
SKFileWStream fs = new("cover.jpg");
bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
bmp.Display(); 

Create New File