Requirement

  • VS Code with Polyglot Notebook
  • .Net Installed

Open VS Code

  • create new File Image.ipynb

Import Skiasharp Library

  • Open Image.ipynb File select .Net Interactive as Kernel
  • Create cell inside File
1
2
3
#r "nuget:SkiaSharp"
using SkiaSharp;
using System.IO;

Image Drawer Function

  • Create New Cell and Paste below code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void DrawImage(string path, SKCanvas canvas, float x = 0, float y = 0)
{
    using (var inputStream = File.OpenRead(path))
    {
        using (var inputBitmap = SKBitmap.Decode(inputStream))
        {
            var imageInfo = new SKImageInfo(inputBitmap.Width, inputBitmap.Height);
            canvas.DrawBitmap(inputBitmap, x , y);
        }
    }
}

Skia SHarp Setup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int width = 1280;
int height = 640; 
SKBitmap bmp = new(width, height);
SKCanvas canvas = new(bmp);
canvas.Clear(SKColor.Parse("#fff"));
SKPaint paint = new()
{
    Color = SKColors.White.WithAlpha(100),
    IsAntialias = true,
    StrokeWidth = 3,
    ColorF = SKColor.Parse("#003366")
};

Uses of Function Created earlier for Importing Image

1
2
3
4
5
6
7
8
DrawImage("image.png", canvas,10, 10);

// For saving the image as a file
using (SKFileWStream fs = new("image.jpg"))
{
    bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
}
bmp.Display()

That all needed to use image in SkiaSharp