Generate Thumbnail

Images on a Skia canvas

Requirements 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

July 20, 2024 · 1 min · 197 words · PrashantUnity
Generate Thumbnail

Triangle illusions

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Triangle class 1 2 3 4 5 6 public class Triangle { public SKPoint A {get;set;} public SKPoint B {get;set;} public SKPoint C {get;set;} } Triangle Point Helper 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 List<Triangle> GetTrianglePoints(int count=10 ,float factor =0.25f,int baseLength=600) { float length= baseLength; var a = new SKPoint((float)Math.Cos(Math.PI/3)*length,0); var b = new SKPoint(0,(float)Math.Sin(Math.PI/3)*length); var c = new SKPoint(length,(float)Math.Sin(Math.PI/3)*length); if(factor>1 || factor<0) factor=0.5f; float m= count*factor; float n= count-m; var ls = new List<Triangle>(); ls.Add(new Triangle() { A=a,B=b,C=c }); for(var i=0; i<count;i++) { var aa = new SKPoint((m*a.X + n*b.X)/count , (m*a.Y + n*b.Y)/count ); var bb = new SKPoint((m*b.X + n*c.X)/count , (m*b.Y + n*c.Y)/count ); var cc = new SKPoint((m*c.X + n*a.X)/count , (m*c.Y + n*a.Y)/count ); var temp = new Triangle() { A = aa, B = bb, C = cc }; ls.Add(temp); a=aa; b=bb; c=cc; } return ls; } 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 // Create an image and fill it blue int width = 1200; int height = 700; int step =50; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 2, ColorF = SKColor.Parse("#003366") }; foreach(var item in GetTrianglePoints(10,.2f,700)) { //paint.ColorF = listOfColor[rand.Next(0,listOfColor.Count)]; canvas.DrawLine(item.A,item.B,paint); canvas.DrawLine(item.B,item.C,paint); canvas.DrawLine(item.C,item.A,paint); } SKFileWStream fs = new("triangleillusion.jpg"); bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50); bmp.Display();

June 23, 2024 · 2 min · 283 words · PrashantUnity
Generate Thumbnail

Basic shapes

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Circle 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int width = 1200; int height = 250; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(0); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 4, ColorF = SKColor.Parse("#003366"), Style = SKPaintStyle.Stroke }; canvas.DrawCircle(110,110,100,paint); // hollow Circle paint.Style = SKPaintStyle.StrokeAndFill; canvas.DrawCircle(410,110,100,paint); // Follow Circle bmp.Display(); Line 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int width = 1200; int height = 20; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(0); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 4, ColorF = SKColor.Parse("#003366") }; SKPoint pointOne = new (0,10); SKPoint pointTwo = new (width,10); canvas.DrawLine(pointOne,pointTwo,paint); bmp.Display(); ...

June 22, 2024 · 3 min · 600 words · PrashantUnity
Generate Thumbnail

Multishape patterns

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Color Sets 1 2 3 4 5 6 7 8 9 10 11 12 var listOfColor = new List<SKColor> { SKColor.Parse("#86B6F6"), SKColor.Parse("#176B87"), SKColor.Parse("#00A9FF"), SKColor.Parse("#FF90BC"), SKColor.Parse("#8ACDD7"), SKColor.Parse("#F2AFEF"), SKColor.Parse("#C499F3"), SKColor.Parse("#33186B"), }; Generate point on circumference of circle of different radius 1 2 3 4 5 6 7 8 9 List<SKPoint> CirclePoints(int n,float radius=3,float x=0, float y=0 ) { float degree = (float)(2*Math.PI/n); return Enumerable .Range(1,n) .Select(i=>degree*i) .Select(i=>(new SKPoint(x+ radius *(float)Math.Cos(i), y+ radius *(float)Math.Sin(i)))) .ToList(); } Section formulae Classic Geometric Formula To Calculate division point on line 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 37 38 39 40 41 42 43 List<(SKPoint,SKPoint)> SectionFormulae(int times, List<SKPoint> points , float factor =0.25f) { var ls = new List<(SKPoint,SKPoint)>(); float m= times*factor; float n= times-m; for(var i=0; i<times;i++) { var templs = new List<SKPoint>(); for(var j=0;j<points.Count;j++) { if(j==points.Count-1) { var x = (m*points[j].X + n*points[0].X)/times; var y = (m*points[j].Y + n*points[0].Y)/times; templs.Add(new SKPoint(x,y)); } else { var x = (m*points[j].X + n*points[j+1].X)/times; var y = (m*points[j].Y + n*points[j+1].Y)/times; templs.Add(new SKPoint(x,y)); } } points = templs; for(var j=0;j<points.Count;j++) { if(j==points.Count-1) { ls.Add((points[j],points[0])); } else { ls.Add((points[j],points[j+1])); } } } return ls; } Granullar Function for more Control Over Shapes 1 2 3 4 public List<(SKPoint,SKPoint)> GetShape(int n=3, int times=6 ,float factor =0.25f,int baseLength=600,int centerX =0,int centerY=0) { return SectionFormulae(times,CirclePoints(n,baseLength,x:centerX,y:centerY),factor); } Skiasharp Codde to Use All above Function and Generate Shapes 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 // Create an image and fill it blue int width = 2000; int height = 2000; int step =50; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 2, ColorF = SKColor.Parse("#003366") }; int seperation=4; for(var i=3;i<=15;i+=3) { paint.Color=listOfColor[rand.Next(0,listOfColor.Count)]; foreach(var item in GetShape(n:i,times:25, factor:0.08f, baseLength:seperation* i*i,centerX:width/2,centerY:height/2)) { canvas.DrawLine(item.Item1,item.Item2,paint); } } // Save the image to disk SKFileWStream fs = new("multishape.jpg"); bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 100); bmp.Display();

June 21, 2024 · 2 min · 375 words · PrashantUnity
Generate Thumbnail

Mosaic in SkiaSharp

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Mosaic Generation Code 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 // Create an image and fill it blue int width = 1920; int height = 700; int step =50; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 2, ColorF = SKColor.Parse("#003366") }; for(var i=0; i<height;i+=step) { for(var j=0; j<width;j+=step) { if(rand.Next(0,100)>50) paint.ColorF =SKColor.Parse("#003366"); else paint.Color =SKColor.Parse("#000f"); canvas.DrawRect(i,j,step,step,paint); } } SKFileWStream fs = new("mosaic.jpg"); bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50); bmp.Display();

June 20, 2024 · 1 min · 121 words · PrashantUnity
Generate Thumbnail

Mazes in SkiaSharp

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Maze Algorithm 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 public class MazeAlgorithm { Random random = new Random(); public int[,] GenerateMaze(int rows, int cols) { int[,] maze = new int[rows, cols]; // Initialize maze with walls for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { maze[i, j] = 1; } } // Set starting point maze[1, 1] = 0; DFS(maze, 1, 1); return maze; } void DFS(int[,] maze, int row, int col) { int[] directions = { 1, 2, 3, 4 }; Shuffle(directions); foreach (int dir in directions) { int[] dRow = { 0, 0, 1, -1 }; int[] dCol = { 1, -1, 0, 0 }; int newRow = row + 2 * dRow[dir - 1]; int newCol = col + 2 * dCol[dir - 1]; if (newRow > 0 && newRow < maze.GetLength(0) - 1 && newCol > 0 && newCol < maze.GetLength(1) - 1 && maze[newRow, newCol] == 1) { maze[row + dRow[dir - 1], col + dCol[dir - 1]] = 0; maze[newRow, newCol] = 0; DFS(maze, newRow, newCol); } } } void Shuffle(int[] array) { int n = array.Length; for (int i = 0; i < n; i++) { int r = i + random.Next(n - i); int temp = array[r]; array[r] = array[i]; array[i] = temp; } } } Skia Sharp To Utilise maze Algorithm and Generate Maze Image 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 // Create an image and fill it blue int width = 1920; int height = 1080; int step =50; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 2, ColorF = SKColor.Parse("#003366") }; int m= width/step, n=height/step; var ls = (new MazeAlgorithm()).GenerateMaze(m,n); ls[0,1]=0; for(var i=0; i<m;i++) { for(var j=0; j<n;j++) { paint.ColorF=(ls[i,j]==0)?SKColor.Parse("#ffffff"):SKColor.Parse("#000000"); canvas.DrawRect(i*step,j*step,step,step,paint); } } SKFileWStream fs = new("maze.jpg"); bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50); bmp.Display();

June 19, 2024 · 2 min · 412 words · PrashantUnity
Generate Thumbnail

Mandelbrot in SkiaSharp

Setup 1 2 #r "nuget:SkiaSharp" using SkiaSharp; Mandelbrot 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 // Create an image and fill it blue int width = 1920; int height = 1080; int step =50; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); Random rand = new(); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 2, ColorF = SKColor.Parse("#003366") }; double zoom = 1; double moveX = -0.5; double moveY = 0; int maxIterations = 50; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX; double zy = (y - height / 2) / (0.5 * zoom * height) + moveY; double cx = zx; double cy = zy; int iteration = 0; double tmp; while ((zx * zx + zy * zy < 4) && (iteration < maxIterations)) { tmp = zx * zx - zy * zy + cx; zy = 2.0 * zx * zy + cy; zx = tmp; iteration++; } if (iteration == maxIterations) { canvas.DrawPoint(x, y, SKColors.Black); } else { // Colorize based on the number of iterations var color = SKColor.FromHsv(iteration % 256, 255, 255); canvas.DrawPoint(x, y, color); } } } SKFileWStream fs = new("mandelbrot.jpg"); bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50); bmp.Display();

June 18, 2024 · 2 min · 284 words · PrashantUnity
Generate Thumbnail

CFD logo in SkiaSharp

Creating Logos with SkiaSharp LOGO on this website is generated using SkiaSharp Only Requirements Visual Studio Code (VS Code) Polyglot Notebook Extension 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 Logo Logo Dimention and Stuff 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int width=700; int height=700; int step = 30; float textSize =128f; string fontFamilyName = "Arial"; string LogoText ="<CFD/>"; float shiftX=150; float dividerX=2.0f; float shiftY=38; float dividerY=3.0f; var left=width/dividerX -shiftX; var top=height/dividerY -shiftY; var right= textSize*3.95f; var bottom=textSize*4f; float targetWidth = 800; // desired width float targetHeight = 800; // desired height float offsetX = 800; // horizontal offset float offsetY = 800; Vapor Structure Path ...

June 17, 2024 · 3 min · 490 words · PrashantUnity
Download Logo

First app in VS (Ch. 2)

Create Your First C# Console Application in Visual Studio Follow these steps to create and run a simple C# console application in Visual Studio. Step 1: Create a New Project Open Visual Studio and click on Create a New Project. Step 2: Choose the Console App Template In the search bar, type Console. Select Console App (C#) from the results. Click Next at the bottom right of the window. ...

June 17, 2024 · 2 min · 248 words · PrashantUnity
Download Logo

First app with CLI (Ch. 2)

Create a C# Console Application Using the CLI Follow these steps to create and run a simple C# console application using the command line. Step 1: Open Terminal and Create a New Project Open your terminal and type the following command to create a new console application project named “HelloWorld”: 1 dotnet new console -o HelloWorld Step 2: Confirm Project Creation After pressing Enter, the command will generate a new project. You should see a confirmation message similar to the image below. ...

June 17, 2024 · 2 min · 256 words · PrashantUnity