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
Generate Thumbnail

Circles & lines in SkiaSharp

Setting Up SkiaSharp 1 #r "nuget:SkiaSharp" Importing namespace to use Skia library 1 using SkiaSharp; Points On circle This function will calculate points on circle and return as list of Points I Depends on number of points we wants to find out on circle Radius of Circle r Center of circle where is it located (x, y) 1 2 3 4 5 6 7 8 9 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)))) .ToArray(); } Line points This Function will first Get all points on circle Then pair the all point with each other as Line Segment Require two Cordinate points one starting point and other end 1 2 3 4 5 6 7 8 9 10 11 12 13 14 List<(SKPoint,SKPoint)> GetAllLinePoints(int n,int radius=3,float x=0, float y=0 ) { var arr =CirclePoints(n:n,x:x,y:y, radius:radius); var ls = new List<(SKPoint, SKPoint)>(); for(var i =0; i<arr.Length; i++) { for(var j=i+1; j<arr.Length;j++) { ls.Add((arr[i],arr[j])); } } return ls; } All Circle This Function will give us all circular points 1 2 3 4 5 6 7 8 9 10 11 12 13 14 List<(float X,float Y)> GetCenter(int m, int n,int radius=60) { var ls =new List<(float X,float Y)>(); int X = radius*3; int Y = radius*3; for(var i=1; i<m;i++) { for(var j=1; j<n;j++) { ls.Add((Y*i,X*j)); } } return ls; } List of random Precalculated hex value 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var listOfColor = new List<SKColor> { SKColor.Parse("#EEF5FF"), SKColor.Parse("#B4D4FF"), SKColor.Parse("#86B6F6"), SKColor.Parse("#176B87"), SKColor.Parse("#00A9FF"), SKColor.Parse("#89CFF3"), SKColor.Parse("#A0E9FF"), SKColor.Parse("#CDF5FD"), SKColor.Parse("#FF90BC"), SKColor.Parse("#FFC0D9"), SKColor.Parse("#F9F9E0"), SKColor.Parse("#8ACDD7"), SKColor.Parse("#F2AFEF"), SKColor.Parse("#C499F3"), SKColor.Parse("#33186B"), }; Final Code to ustiliase all above function 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 // Image size (pixels) int WIDTH = 1920; int radius =60; Random random = new Random(); SKBitmap bmp = new(1280, WIDTH); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse("#fff")); SKPaint paint = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true , StrokeWidth = 1, ColorF = SKColor.Parse("#000000"), Style = SKPaintStyle.Stroke }; var ls = GetCenter(10,7,radius); for(var n=3;n<=26;n++) { float X = ls[n-3].Y; float Y = ls[n-3].X; paint.ColorF =listOfColor[random.Next(0,listOfColor.Count)]; foreach(var i in GetAllLinePoints(n:n,x:X,y:Y, radius:radius)) { canvas.DrawLine(i.Item1,i.Item2,paint); canvas.DrawText($"{n} Points",X-20,Y+80,paint); } canvas.DrawCircle(X,Y,60,paint); } bmp.Display() Image Generated using above Code

August 3, 2024 · 2 min · 403 words · PrashantUnity
Download Logo

SkiaSharp + Blazor WASM

Implementation 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 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 @page "/" @using SkiaSharp @using SkiaSharp.Views.Blazor <button @onclick="ButtonClicked">Redraw Image</button> <SKCanvasView @ref="canvasReference" OnPaintSurface="@OnPaintSurface" style="@($"height: {1920}px; width: {1080}px;")" IgnorePixelScaling=true /> @code { SKCanvasView canvasReference; void ButtonClicked() { canvasReference.Invalidate(); } private void OnPaintSurface(SKPaintSurfaceEventArgs e) { var canvas = e.Surface.Canvas; canvas.Clear(SKColor.Parse("#003366")); int width = 300; int height = 300; int step = 12; SKBitmap bmp = new(width, height); Random rand = new(0); SKPaint paintR = new() { Color = SKColors.White.WithAlpha(100), IsAntialias = true }; for (var i = 0; i < width; i = i + step) { for (var j = 0; j < height; j = j + step) { paintR.StrokeWidth = rand.Next(1, 6); Draw(i, j, step, step, paintR, canvas); } } } void Draw(int x, int y, int width, int height, SKPaint paint, SKCanvas canvas) { Random random = new Random(); paint.Color = listOfColor[random.Next(0, listOfColor.Count)]; var prob = random.Next(0, 10); if (prob < 5) { SKPoint pointOne = new(x, y); SKPoint pointTwo = new(x + width, y + height); //paint.StrokeWidth = rand.Next(1, 10); canvas.DrawLine(pointOne, pointTwo, paint); //canvas.DrawCircle(pointOne, random.Next(1, 6), paint); } else { SKPoint pointOne = new(x + width, y); SKPoint pointTwo = new(x, y + height); //paint.StrokeWidth = rand.Next(1, 10); canvas.DrawLine(pointOne, pointTwo, paint); } } List<SKColor> listOfColor = new List<SKColor> { SKColor.Parse("#EEF5FF"), SKColor.Parse("#B4D4FF"), SKColor.Parse("#86B6F6"), SKColor.Parse("#176B87"), SKColor.Parse("#00A9FF"), SKColor.Parse("#89CFF3"), SKColor.Parse("#A0E9FF"), SKColor.Parse("#CDF5FD"), SKColor.Parse("#FF90BC"), SKColor.Parse("#FFC0D9"), SKColor.Parse("#F9F9E0"), SKColor.Parse("#8ACDD7"), SKColor.Parse("#F2AFEF"), SKColor.Parse("#C499F3"), SKColor.Parse("#33186B"), }; }

July 12, 2024 · 2 min · 304 words · PrashantUnity